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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Trigger insertion cascade

Author  Topic 

fares47
Starting Member

2 Posts

Posted - 2011-11-26 : 10:10:21
Hi every body i want to create a trigger for insertion cascade in other words i have for example 2 tables T1 and T2 that have relationship between them and each table have a structure like this : T1(idstud(pk),name,age) T2(idstud(fk),grade,...)
and when i insert a new row in T1 the trigger will be able to add same idStud to T2 (just idStud) Thanks in advance

I Want to believe in what i know

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-26 : 10:19:42
it will be like


CREATE TRIGGER InsertT2Data
ON T1
FOR INSERT
AS
BEGIN
INSERT T2
SELECT idstud,grade,..
FROM INSERTED
END


I would have handled this in insert procedure itself though to improve maintainability

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

fares47
Starting Member

2 Posts

Posted - 2011-11-26 : 11:27:37
Thanks but when Trigger is fired and i have a little surprise :
when i run select against T2 it display idStud twice for example when i add idStud =1 to table T1 trigger add twice the same value :

T1 ------ idStud ------

1

T2 ------ idStud ------

1
1

I don't understand why ?!

I Want to believe in what i know
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-26 : 12:17:50
quote:
Originally posted by fares47

Thanks but when Trigger is fired and i have a little surprise :
when i run select against T2 it display idStud twice for example when i add idStud =1 to table T1 trigger add twice the same value :

T1 ------ idStud ------

1

T2 ------ idStud ------

1
1

I don't understand why ?!

I Want to believe in what i know


can you show your insertion code?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -