Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Row Update SQL Trigger from Single Update SQL Statement

Ok. I am quite new to SQL triggers and have had some issues with them. Insert trigger works just fine, and the Delete trigger also. At first, doing a delete on multiple rows would only delete one, but I managed to figure that one out for myself :)

However, even after extensive searching (here and on Google) I am unable to find a satisfactory answer to the UPDATE trigger that I have. If I do an update like

UPDATE Customers Set CustomerUser = 0 Where CustomerStatus = 3

Then unfortunately, only the one record would be updated, and the other would remain as they were. Obviously, this is no good.

The trigger I am using is:

ALTER TRIGGER [dbo].[TRG_TriggerName] ON [dbo].[USER_Customers]

FOR UPDATE

AS
declare @customerid int;
declare @customervenue int;
declare @customeruser int;
declare @customerarea int;
declare @customerevent int;
declare @customerproject int;
declare @customerstatus int;

select @customerid=i.CustomerID from inserted i;
select @customervenue=i.CustomerVenue from inserted i;
select @customerarea=i.CustomerArea from inserted  i;
select @customerevent=i.CustomerEvent from inserted i;
select @customerproject=i.CustomerProject from inserted i;
select @customeruser=i.CustomerUser from inserted i;
select @customerstatus=i.CustomerStatus from inserted i;

Update USER_Instances Set InstanceArea = @customerarea, InstanceVenue = @customervenue, InstanceUser = @customeruser, InstanceStatus = @customerstatus, InstanceEvent = @customerevent, InstanceLastUpdate = GetDate() Where InstanceObject = 17 AND InstanceIdentity = @customerid
GO

As you will immediately realize, this trigger is great - if you want to update just one record. Otherwise, it fails. Now - the main question here would be - How do I catch all the records that need updating, and update them all in one trigger action.

The examples I have seen here on Stack Overflow confuse me somewhat, or seem ineffective - for instance, it seems most of them deal with updating just ONE value in a second/other table, and not a whole bunch like I am trying to do. The ones that appear to work on multiple values, I can not understand :(

So after about 2 hours of searches, I give up, and hope that you can help me :) I realize this is a trigger-newbie issue, and though I know my MS-SQL, triggers are something I have never used, until now. So any help is greatly welcome :) W

like image 957
Irresistance Avatar asked May 18 '13 13:05

Irresistance


People also ask

Can we UPDATE multiple rows in a single UPDATE statement in SQL?

Learn MySQL from scratch for Data Science and AnalyticsColumn values on multiple rows can be updated in a single UPDATE statement if the condition specified in WHERE clause matches multiple rows. In this case, the SET clause will be applied to all the matched rows.

How do you UPDATE multiple records for a single column in SQL?

First, specify the table name that you want to change data in the UPDATE clause. Second, assign a new value for the column that you want to update. In case you want to update data in multiple columns, each column = value pair is separated by a comma (,). Third, specify which rows you want to update in the WHERE clause.

Can we use two triggers on same table before after UPDATE?

Yes, you can definitely have more than one trigger for each operation, e.g. AFTER INSERT or AFTER UPDATE etc. It does make sense to split up separate concerns into separate, small, manageable chunks of code.

Can we use UPDATE command in trigger?

AFTER UPDATE Trigger is a kind of trigger in SQL that will be automatically fired once the specified update statement is executed. It can be used for creating audit and log files which keep details of last update operations on a particular table.


1 Answers

It seems that you need something like this

ALTER TRIGGER [dbo].[TRG_TriggerName] ON [dbo].[USER_Customers]
FOR UPDATE
AS
UPDATE USER_Instances
   SET InstanceArea = i.CustomerArea, 
       InstanceVenue = i.CustomerVenue, 
       InstanceUser = i.CustomerUser, 
       InstanceStatus = i.CustomerStatus, 
       InstanceEvent = i.CustomerEvent, 
       InstanceLastUpdate = GetDate() 
  FROM USER_Instances JOIN inserted i
    ON InstanceIdentity = i.CustomerID AND InstanceObject = 17

Since inserted virtual table can contain multiple rows you need to JOIN it to correctly do your UPDATE.

like image 56
peterm Avatar answered Nov 16 '22 02:11

peterm