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
 Trigger FOR UPDATE

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 T2
T1 links to T3

I need to SET dbo.T3.FT3= dbo.T3.FT3+ ‘ABC’ if dbo.T2.FT2 = True
I have tried:
CREATE TRIGGER [dbo].[TrigT3]
ON [dbo].[T3]
FOR UPDATE
AS
BEGIN
SET NOCOUNT ON;
UPDATE dbo.T3
SET dbo.T3.FT3toupd= FT3toupd+ ‘abc’
FROM dbo.T3, dbo.T1, dbo.T2
WHERE 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.T1ID
WHERE (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))
END

My syntax appears to be correct as the trigger is created but it does not fire if dbo.T2.FT2_Conditional is = True

Any help would be appreciated
Thanks,
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 UPDATE
AS
BEGIN
SET NOCOUNT ON;
UPDATE t3
SET t3.FT3toupd= t3.FT3toupd+ ‘abc’
FROM dbo.T3 t3
INNER JOIN dbo.T1 t1
ON t1.T1ID = t3.T1ID
INNER JOIN INSERTED i
ON 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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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]
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2010-11-19 : 12:13:30
>> if dbo.T2.FT2 = True
You 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.
Go to Top of Page

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 ...
Go to Top of Page

Tanja
Starting Member

4 Posts

Posted - 2010-11-19 : 14:21:40

Thank you very much visakh16
My trigger is working properly now

Tanja

quote:
Originally posted by visakh16

seems like what you need is this?


CREATE TRIGGER [dbo].[TrigT3]
ON [dbo].[T2]
FOR UPDATE
AS
BEGIN
SET NOCOUNT ON;
UPDATE t3
SET t3.FT3toupd= t3.FT3toupd+ ‘abc’
FROM dbo.T3 t3
INNER JOIN dbo.T1 t1
ON t1.T1ID = t3.T1ID
INNER JOIN INSERTED i
ON 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 MVP
http://visakhm.blogspot.com/





tanja
Go to Top of Page

Tanja
Starting Member

4 Posts

Posted - 2010-11-19 : 14:24:18
Thank you very much
Tanja
quote:
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
Go to Top of Page

Tanja
Starting Member

4 Posts

Posted - 2010-11-19 : 14:25:23
Thank you
Tanja

quote:
Originally posted by nigelrivett

>> if dbo.T2.FT2 = True
You 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
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-11-21 : 03:43:53
welcome

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

Go to Top of Page
   

- Advertisement -