| Author |
Topic |
|
Tanja
Starting Member
4 Posts |
Posted - 2010-11-19 : 12:07:11
|
| Hi,I am using SQL 2005 and was wondering if I can create a trigger to update a field when a field from a not directly linked table is set to True.I have 3 tables:T1 links to T2T1 links to T3 I need to SET dbo.T3.FT3= dbo.T3.FT3+ ‘ABC’ if dbo.T2.FT2 = TrueI have tried:CREATE TRIGGER [dbo].[TrigT3] ON [dbo].[T3] FOR UPDATEAS BEGINSET NOCOUNT ON; UPDATE dbo.T3SET dbo.T3.FT3toupd= FT3toupd+ ‘abc’FROM dbo.T3, dbo.T1, dbo.T2WHERE EXISTS (SELECT dbo.T3.T3ID, dbo.T3.FT3_1, dbo.FT3. FT3toupd, dbo.T3. FT3_2, dbo.T3. FT3_3, dbo.T1.T1ID, dbo.T2.T2ID, dbo.T3. FT3_4, dbo.T2.FT2_Conditional (when this field is set to True the trigger should fire)FROM dbo.T2 RIGHT OUTER JOIN dbo.T1 ON dbo.T2.T1ID = dbo.T1.T1ID LEFT OUTER JOIN dbo.T3 ON dbo.T1.T1ID = dbo.T3.T1IDWHERE (NOT (dbo.T3.T3ID IS NULL)) AND (NOT (dbo.T3. dbo.T3.FT3_1 IS NULL)) AND (dbo.T3. dbo.T3.FT3_1 <> '') AND (dbo.T3. dbo.T3.FT3_5 = 2) AND (dbo.T2. FT2_Conditional = 1))ENDMy syntax appears to be correct as the trigger is created but it does not fire if dbo.T2.FT2_Conditional is = TrueAny help would be appreciatedThanks,Tanja |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-11-19 : 12:12:39
|
seems like what you need is this?CREATE TRIGGER [dbo].[TrigT3]ON [dbo].[T2]FOR UPDATEAS BEGINSET NOCOUNT ON;UPDATE t3SET t3.FT3toupd= t3.FT3toupd+ ‘abc’FROM dbo.T3 t3INNER JOIN dbo.T1 t1ON t1.T1ID = t3.T1IDINNER JOIN INSERTED iON i.T1ID = t1.T1ID WHERE (NOT (t3.T3ID IS NULL)) AND (NOT (t3.FT3_1 IS NULL)) AND (t3.FT3_1 <> '') AND (t3.FT3_5 = 2) AND (i. FT2_Conditional = 1))END ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-11-19 : 12:13:18
|
| "My syntax appears to be correct as the trigger is created but it does not fire if dbo.T2.FT2_Conditional is = True"Trigger needs to be on [T2] table then, not on [T3] |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-11-19 : 12:13:30
|
| >> if dbo.T2.FT2 = TrueYou mean you want to trigger the update when this is changed to true?If so then the trigger needs to be on T2.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-11-19 : 12:14:51
|
- what's the collective noun for a queue of Yaks? A Snipe of Yaks perhaps ... |
 |
|
|
Tanja
Starting Member
4 Posts |
Posted - 2010-11-19 : 14:21:40
|
Thank you very much visakh16My trigger is working properly now Tanjaquote: Originally posted by visakh16 seems like what you need is this?CREATE TRIGGER [dbo].[TrigT3]ON [dbo].[T2]FOR UPDATEAS BEGINSET NOCOUNT ON;UPDATE t3SET t3.FT3toupd= t3.FT3toupd+ ‘abc’FROM dbo.T3 t3INNER JOIN dbo.T1 t1ON t1.T1ID = t3.T1IDINNER JOIN INSERTED iON i.T1ID = t1.T1ID WHERE (NOT (t3.T3ID IS NULL)) AND (NOT (t3.FT3_1 IS NULL)) AND (t3.FT3_1 <> '') AND (t3.FT3_5 = 2) AND (i. FT2_Conditional = 1))END ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
tanja |
 |
|
|
Tanja
Starting Member
4 Posts |
Posted - 2010-11-19 : 14:24:18
|
Thank you very muchTanjaquote: Originally posted by Kristen "My syntax appears to be correct as the trigger is created but it does not fire if dbo.T2.FT2_Conditional is = True"Trigger needs to be on [T2] table then, not on [T3]
tanja |
 |
|
|
Tanja
Starting Member
4 Posts |
Posted - 2010-11-19 : 14:25:23
|
Thank you Tanjaquote: Originally posted by nigelrivett >> if dbo.T2.FT2 = TrueYou mean you want to trigger the update when this is changed to true?If so then the trigger needs to be on T2.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
tanja |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-11-21 : 03:43:53
|
welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|