How can I do a Trigger in SQL? -


i trying learn triggers, know how basic ones, can't wrap head around this. have 2 tables services(master) , sales.

services(serviceid,servicecost,salestotal) sales(transactionid,transactiondate,amount,serviceid) 

i trying write trigger update,delete,insert. when ever enter new sale in sales table, salestotal updated in services table according serviceid.

ex:

insert sales(transactionid,transactiondate,amount,serviceid) values ('16','2014-11-19','50','101'); 

so if salestotal transactionid '101' 1000, after insert 1050 , opposite if deleted/updated.

i think have use join tables, stumped.

use trigger, or split insert / dalete / update: code:

  create trigger [dbo].[t_update_services] on sales after update,insert, delete

as

begin

update set salestotal = salestotal - b.amount services join deleted b on a.serviceid = b.serviceid

update set salestotal = salestotal + b.amount services join inserted b on a.serviceid = b.serviceid

end


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -