SQL Server update value in another table if value in (inserted) first table is greater - sql

I have the following tables: (simplified for clarity)
AppWorkHeader with columns: (ProjectID (FK), ProgPercent, + 40 others not relevant)
AppProjects with columns: (ProjectID (PK), ProgPercent + 9 others not relevant)
I am trying to create a trigger that after insert, the ProgPercent value from the AppWorkHeader table updates the ProgPercent value in the AppProjects table only if it's greater than the existing value. I can get it to work with single insertions with the following:
-- Only works on single row insert
CREATE TRIGGER [dbo].[AppWorkHeader_project_trigger]
ON
[dbo].[AppWorkHeader]
AFTER INSERT AS
IF ##ROWCOUNT = 0
RETURN
SET NOCOUNT ON
-- Get the current project completion percentage from the AppProjects table
DECLARE #inserted_projectID int = (SELECT i.ProjectID FROM inserted i)
DECLARE #current_percent decimal(5,4) = (SELECT ProgPercent FROM AppProjects WHERE ProjectID = #inserted_projectID)
UPDATE AppProjects
SET ProgPercent = inserted.ProgPercent
FROM inserted
WHERE AppProjects.ProjectID = inserted.ProjectID AND inserted.ProgPercent > #current_percent
I can get multiple insertions to work with the following code. However, the greater-than part of my where clause seems to be ignored. Multiple insertions with the same ProjectID are updated to lower values.
-- Multiple row insert
CREATE TRIGGER [dbo].[AppWorkHeader_project_trigger]
ON
[dbo].[AppWorkHeader]
AFTER INSERT AS
IF ##ROWCOUNT = 0
RETURN
SET NOCOUNT ON
UPDATE AppProjects
SET ProgPercent = inserted.ProgPercent
FROM inserted
WHERE AppProjects.ProjectID = inserted.ProjectID AND inserted.ProgPercent > (SELECT ProgPercent FROM AppProjects WHERE ProjectID = (SELECT inserted.ProjectID))
I can't figure out how to get the existing value in the AppProjects table without using a variable, and I can't seem to get multiple insertions to work if I use a variable. Where am I going wrong?

For each inserted ProjectID find the maximum ProgPercent (if there are several rows inserted with the same ProjectID).
Then join table with new values to the table with old values.
CREATE TRIGGER [dbo].[AppWorkHeader_project_trigger]
ON [dbo].[AppWorkHeader]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
WITH
CTE_InsertedProjects
AS
(
SELECT
inserted.ProjectID
,MAX(inserted.ProgPercent) AS MaxProgPercent
FROM inserted
GROUP BY inserted.ProjectID
)
,CTE_AllProjects
AS
(
SELECT
AppProjects.ProjectID
,AppProjects.ProgPercent AS OldProgPercent
,CTE_InsertedProjects.MaxProgPercent AS NewProgPercent
FROM
AppProjects
INNER JOIN CTE_InsertedProjects ON CTE_InsertedProjects.ProjectID = AppProjects.ProjectID
WHERE
CTE_InsertedProjects.MaxProgPercent > AppProjects.ProgPercent
)
UPDATE CTE_AllProjects
SET OldProgPercent = NewProgPercent;
END

While Tring to Update Use JOIN To match the column ProjectID from AppProjects on AppWorkHeader ..
Update AP
set AP.ProgPercent=AWH.ProgPercent
FROM AppProjects AP Left JOIN AppWorkHeader AWH on AP.ProjectID=AWH.ProjectID
Where AP.ProgPercent < AWH.ProgPercent
This will only Update those rows where ProgPercent in AppProjects is Lesser than ProgPercent in AppWorkHeader

Related

Enumerate the multiple rows in a multi-update Trigger

I have something like the table below:
CREATE TABLE updates (
id INT PRIMARY KEY IDENTITY (1, 1),
name VARCHAR (50) NOT NULL,
updated DATETIME
);
And I'm updating it like so:
INSERT INTO updates (name, updated)
VALUES
('fred', '2020-11-11),
('fred', '2020-11-11'),
...
('bert', '2020-11-11');
I need to write an after update Trigger and enumerate all the name(s) that were added and add each one to another table but can't work out how enumerate each one.
EDIT: - thanks to those who pointed me in the right direction, I know very little SQL.
What I need to do is something like this
foreach name in inserted
look it up in another table and
retrieve a count of the updates a 'name' has done
add 1 to the count
and update it back into the other table
I can't get to my laptop at the moment, but presumably I can do something like:
BEGIN
SET #count = (SELECT UCount from OTHERTAB WHERE name = ins.name)
SET #count = #count + 1
UPDATE OTHERTAB SET UCount = #count WHERE name = ins.name
SELECT ins.name
FROM inserted ins;
END
and that would work for each name in the update?
Obviously I'll have to read up on set based SQL processing.
Thanks all for the help and pointers.
Based on your edits you would do something like the following... set based is a mindset, so you don't need to compute the count in advance (in fact you can't). It's not clear whether you are counting in the same table or another table - but I'm sure you can work it out.
Points:
Use the Inserted table to determine what rows to update
Use a sub-query to calculate the new value if its a second table, taking into account the possibility of null
If you are really using the same table, then this should work
BEGIN
UPDATE OTHERTAB SET
UCount = COALESCE(UCount,0) + 1
WHERE [name] in (
SELECT I.[name]
FROM Inserted I
);
END;
If however you are using a second table then this should work:
BEGIN
UPDATE OTHERTAB SET
UCount = COALESCE((SELECT UCount+1 from OTHERTAB T2 WHERE T2.[name] = OTHERTAB.[name]),0)
WHERE [name] in (
SELECT I.[name]
FROM Inserted I
);
END;
Using inserted and set-based approach(no need for loop):
CREATE TRIGGER trg
ON updates
AFTER INSERT
AS
BEGIN
INSERT INTO tab2(name)
SELECT name
FROM inserted;
END

Creating INSTEAD OF UPDATE trigger to update HasChanged field in 3 tables

I've created a transform view that updates the master table with values from two different tables.
When doing this update, I need to change the 'HasChanged' value in all the tables for the affected records.
Ex: One row in the master table is updated with road name from one table and city name from another. The rows in the 2 tables that contained the new road name and city name must have their HasChanged value updated to 0 (from 1), as well as the master table.
For this I need to create a trigger. So far this is what I have:
CREATE TRIGGER [Transform].[trItems_HasChanged] ON [Transform].[vItems_HasChanged]
INSTEAD OF UPDATE
AS
BEGIN
SET NOCOUNT ON
CREATE TABLE #inserted
(
Id BIGINT
)
CREATE TABLE #PostnummerBy
(
Postnr BIGINT
)
CREATE TABLE #Vejstykke
(
Id BIGINT
)
INSERT INTO #Inserted (Infohub_Id)
SELECT Id FROM inserted
CREATE NONCLUSTERED INDEX ixInserted_Infohub_Id ON #Inserted (Infohub_Id)
UPDATE
InfohubStaging.Perfion.tMirror_Items
SET
Infohub_HasChanged = 0
FROM
InfohubStaging.Perfion.tMirror_Items
JOIN #Inserted i ON i.Infohub_Id = InfohubStaging.Perfion.tMirror_Items.Infohub_Id
WHERE
Infohub_HasChanged = 1
UPDATE
InfohubStaging.Nav.tMirror_PimItem
SET
Infohub_HasChanged = 0
FROM
InfohubStaging.Nav.tMirror_PimItem
JOIN #Inserted i ON i.Infohub_Id = InfohubStaging.Nav.tMirror_PimItem.InfohubId
WHERE
Infohub_HasChanged = 1
UPDATE
InfohubStaging.Ax.tMirror_Items_LiveUpdates
SET
Infohub_HasChanged = 0
FROM
InfohubStaging.Ax.tMirror_Items_LiveUpdates
JOIN #Inserted i ON i.Infohub_Id = InfohubStaging.Ax.tMirror_Items_LiveUpdates.Infohub_Id
WHERE
Infohub_HasChanged = 1
END
I am unsure how to correctly go about this. I'm aware that 2 temp tables are created when doing an update trigger (the data before and after). Since the temp tables are non-indexable, I need to create an index for them. So I need to create 3 tables, one for each of the tables who need to have their HasChanged value updated.
My expected output from this has the HasChanged value updated in the Vejstykke, PostnummerBy and Master table.

Delete trigger and getting field from another table

I have this delete trigger on an SQL database. The record deletes currently and gets written to an audit table. I have been asked to include in this history table a field from another table that is related to the record being deleted based on SurveyID. I thought I could do something like
select #Status = Status from table where Survey = deleted.Survey
But this is incorrect syntax.
ALTER trigger [dbo].[table_Selfdelete]
on [dbo].[table]
after delete
as
Begin
Set nocount on;
Declare #SurveyId int
Declare #StudentUIC varchar(10)
Declare #Status varchar(10)
select #SurveyId = deleted.SurveyID,
#StudentUIC = deleted.StudentUIC
from deleted
select #Status = Status from tbly when SurveyID = deleted.SurveyID
insert into fupSurveyAudit
values(#SurveyId,#StudentUIC,#Status)
End
Arrgh. I think you want this insert in your trigger (and nothing else):
insert into fupSurveyAudit(SurveyId, StudentUIC, status)
select d.SurveyId, d.StudentUIC, y.status
from deleted d left join
tbly y
on d.SurveyId = y.SurveyId;
Notes:
deleted could contain more than one row, so assuming that it has one row can lead to a run-time error or incorrect results.
A left join is needed in case there is no matching row for the status.
You should always include the columns in an insert
Your archive table should have additional columns, such as an identity column and the date of the insert, which are set automatically (and hence not explicitly part of the insert).
Triggers are fired once for each statement (Delete,insert,update) not for each row inside the statement.
You cannot use variables here because when multiple lines are deleted from the table only one line will be inserted in the Audit table because the variable can only hold one value.
You just need a simple insert from the deleted table into the Audit table something like this....
ALTER trigger [dbo].[table_Selfdelete]
on [dbo].[table]
after delete
as
Begin
Set nocount on;
insert into fupSurveyAudit(SurveyId, StudentUIC,[Status])
select d.SurveyID
,d.StudentUIC
,y.[Status]
from deleted d
INNER JOIN tbly y ON y.SurveyID = deleted.SurveyID
End
Try this
ALTER trigger [dbo].[table_Selfdelete]
on [dbo].[table]
after delete
as
Begin
Set nocount on;
insert into fupSurveyAudit -- Better listed the column list here
select
d.SurveyID, d.StudentUIC, y.Status
from
deleted d JOIN tbly y ON d.SurveyID = y.SurveyID
End

After insert not working

all id columns has auto_increment
In my trigger:
ALTER trigger [dbo].[mytrig]
on [dbo].[requests]
after INSERT, UPDATE
as
begin
declare #MyId1 int
set #MyId1 = (select Id from inserted)
declare #MyId2 int
declare #MyId3 int
if (select column1 from inserted) = 1
begin
insert into [dbo].[contracts] select column1,column2,column3 .... from inserted
set #MyId2 = SCOPE_IDENTITY()
insert into [dbo].[History] select column1,column2,column3 .... from inserted
set #MyId3 = SCOPE_IDENTITY()
insert into [dbo].[contracts_depts](Id_Contract ,column5) select #MyId2,column6 from request_depts where Id_request=#MyId1
insert into [dbo].[History_depts] (Id_InHistory,column5) select #MyId3,column6 from request_depts where Id_request=#MyId1
end
end
#MyId1 returns value only after update but not after insert. Do I have to use scope_identity() or something ?
Your main issue is: you're assuming the triggers is called once per row - that is NOT the case!
The trigger is called once per statement, and if your statement affects multiple rows, the Inserted pseudo table will contain multiple rows - so your statement here
set #MyId1 = (select Id from inserted)
really isn't going to work - it will select one arbitrary row (out of however many there are).
You'll need to rewrite your trigger to take this fact into account! Assume that Inserted contains 100 rows - how do you want to deal with that? What are you trying to achieve? Triggers don't return values - they will record into an audit table, or update other rows, or something like that ....

Sometimes SQL Server trigger is not firing

I created a trigger on my recharge table. It updates the balance of onaccountregistry table.
But sometimes when inserting rows into my recharge table it is not firing the trigger. Then values are mismatch. This recharge table insert rows every time.
I created the trigger as follows. This is not a replicated table. I'm using SQL Server 2008 Enterprise edition.
Please help me solve this matter
CREATE TRIGGER [dbo].[RechargeRefund]
ON [dbo].[ISRecharge]
FOR INSERT
AS
declare #tin char(9)
declare #depocd char(1)
declare #oldvalue money
declare #newvalue money
begin
select #tin = inserted.tin_subtin from inserted
select #depocd = inserted.updatetype from inserted
select #newvalue = inserted.DepositAmt from inserted
select #oldvalue = Totdeposit from ISOnAcctRegistry where tin_subtin = #tin
end
if #depocd ='1'
begin
update ISOnAcctRegistry
set Totdeposit = #oldvalue + #newvalue
where tin_subtin = #tin
end
if #depocd ='2'
begin
update ISOnAcctRegistry
set Totdeposit = #oldvalue - #newvalue
where tin_subtin = #tin
end
GO
As #marc points out, writing assuming a single row in inserted is bad - it can even be possible for your 3 selects from inserted to assign values to your 3 variables from 3 different (arbitrary) rows from inserted.
What you probably want is:
update i1
set Totdeposit = Totdesposit + t2.Total
from ISOnAcctRegistry i1
inner join
(select
tin_subtin,
SUM(CASE updatetype
WHEN 1 THEN DepositAmt
WHEN 2 THEN -DepositAmt
ELSE 0 END) as Total
from inserted
group by tin_subtin) t2
on
i1.tin_subtin = t2.tin_subtin
But you might be able to replace this work (and this column in ISOnAcctRegistry) with an indexed view built on ISRecharge - with some limitations, you can build a view that automatically performs a SUM across the rows in ISRecharge, and SQL Server would take responsibility for maintaining the value in the background for you.
Obviously, at present, your trigger doesn't account for any UPDATE or DELETE activity on ISRecharge. An indexed view would.
Well, of course it won't work - you're assuming that the trigger fires once per row inserted.
But that's NOT the case.
The trigger fires once per INSERT batch, and the pseudo-table Inserted might contain multiple rows!
If you did get an INSERT with more than one row - which row did your statements here select ?
select #tin = inserted.tin_subtin from inserted
select #depocd = inserted.updatetype from inserted
select #newvalue = inserted.DepositAmt from inserted
select #oldvalue = Totdeposit from ISOnAcctRegistry where tin_subtin = #tin
You need to rewrite your trigger so that it will handle multiple rows in Inserted - then it'll work every time.