Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 MSSQL Trigger.

Author  Topic 

Gerald30
Yak Posting Veteran

62 Posts

Posted - 2012-09-26 : 05:20:19
Hello All,

I really need some help on how to create triggers in MS SQL.

Previously we are using MySQL and now we need to migrate to MS SQL.

Can some one teach me on how to create triggers in MSSQL?

I have a made a trigger that do this

TableA and Table B.


If I insert Name and Age to Table A it automatically creates a copy to Table B.

I have use this syntax.



create trigger tig1 on TableA
for insert as
insert into TableB(name2,age2) select name,age
from inserted

go


Now next thing that I want to do is when you update the Name or Age in TableA it will also update in TableB.


I will really appriciate any of your help.

Thank you.

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-09-26 : 05:25:01
Is there a key column to match rows from tableA with tableB?


Too old to Rock'n'Roll too young to die.
Go to Top of Page

Gerald30
Yak Posting Veteran

62 Posts

Posted - 2012-09-26 : 20:55:45
Oh Sorry I forgot to include the EID.

TableA hase EID,Name,Age while be TableB has EID2,Name2 and Age2
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-09-27 : 02:35:45
create trigger tig2 on TableA
for update as
update t2
set name2 = i.name, age2 = i.age
from TableB as t2
join inserted as i on i.EID = t2.EID2
go


Too old to Rock'n'Roll too young to die.
Go to Top of Page
   

- Advertisement -