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 2005 Forums
 Transact-SQL (2005)
 Help with Update Trigger

Author  Topic 

raxbat
Yak Posting Veteran

52 Posts

Posted - 2007-09-26 : 02:38:34
Hello all!
I need help from You experts!

I need to make an update trigger!
For example we have table with fields: Value1,Value2,LastChange.
The trigger have to make LastChange=GetDate() if on the update the values of Value1 or Value2 were changed!

Help me please!!!!!

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-26 : 02:51:27
[code]CREATE TRIGGER trgTable1Update ON Table1
FOR UPDATE

IF UPDATE(Value1) OR UPDATE(Value2)
UPDATE t1
SET t1.LastChange = CURRENT_TIMESTAMP
FROM Table1 AS t1
INNER JOIN inserted AS i ON i.PkCol = t1.PkCol
INNER JOIN deleted AS d ON d.PkCol = t1.PkCol
WHERE i.Value1 <> d.Value1
OR i.Value2 <> d.Value2[/code]

E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

raxbat
Yak Posting Veteran

52 Posts

Posted - 2007-09-26 : 03:02:46
Thanx a lot!
Go to Top of Page

raxbat
Yak Posting Veteran

52 Posts

Posted - 2007-09-26 : 03:06:26
Script doesnt run beacouse of the following error:
"Error 156" incorect syntax near word IF and OR
Go to Top of Page

raxbat
Yak Posting Veteran

52 Posts

Posted - 2007-09-26 : 03:11:51
Got it!!!
Go to Top of Page
   

- Advertisement -