SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 MSSQL Trigger.
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Gerald30
Yak Posting Veteran

Philippines
52 Posts

Posted - 09/26/2012 :  05:20:19  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

Germany
8768 Posts

Posted - 09/26/2012 :  05:25:01  Show Profile  Visit webfred's Homepage  Reply with Quote
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

Philippines
52 Posts

Posted - 09/26/2012 :  20:55:45  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

Germany
8768 Posts

Posted - 09/27/2012 :  02:35:45  Show Profile  Visit webfred's Homepage  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000