sql server - Multiple Row Update SQL Trigger from Single Update SQL Statement -


ok. quite new sql triggers , have had issues them. insert trigger works fine, , delete trigger also. @ first, doing delete on multiple rows delete one, managed figure 1 out myself :)

however, after extensive searching (here , on google) unable find satisfactory answer update trigger have. if update like

update customers set customeruser = 0 customerstatus = 3 

then unfortunately, 1 record updated, , other remain were. obviously, no good.

the trigger using is:

alter trigger [dbo].[trg_triggername] on [dbo].[user_customers]  update  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 inserted i; select @customervenue=i.customervenue inserted i; select @customerarea=i.customerarea inserted  i; select @customerevent=i.customerevent inserted i; select @customerproject=i.customerproject inserted i; select @customeruser=i.customeruser inserted i; select @customerstatus=i.customerstatus inserted i;  update user_instances set instancearea = @customerarea, instancevenue = @customervenue, instanceuser = @customeruser, instancestatus = @customerstatus, instanceevent = @customerevent, instancelastupdate = getdate() instanceobject = 17 , instanceidentity = @customerid go 

as realize, trigger great - if want update 1 record. otherwise, fails. - main question here - how catch records need updating, , update them in 1 trigger action.

the examples have seen here on stack overflow confuse me somewhat, or seem ineffective - instance, seems of them deal updating 1 value in second/other table, , not whole bunch trying do. ones appear work on multiple values, can not understand :(

so after 2 hours of searches, give up, , hope can me :) realize trigger-newbie issue, , though know ms-sql, triggers have never used, until now. welcome :) w

it seems need this

alter trigger [dbo].[trg_triggername] on [dbo].[user_customers] update update user_instances    set instancearea = i.customerarea,         instancevenue = i.customervenue,         instanceuser = i.customeruser,         instancestatus = i.customerstatus,         instanceevent = i.customerevent,         instancelastupdate = getdate()    user_instances join inserted     on instanceidentity = i.customerid , instanceobject = 17 

since inserted virtual table can contain multiple rows need join correctly update.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -