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)
 auto Update DateTime field

Author  Topic 

stroek
Starting Member

18 Posts

Posted - 2008-12-16 : 05:51:39
Hello,

How can I create a field that updates every time the record has bin updated

Regards

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-12-16 : 06:00:06
You can use a trigger to update the column for you.
Your best bet is probably an "INSTEAD OF" trigger.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

stroek
Starting Member

18 Posts

Posted - 2008-12-16 : 06:14:26
Tks,

When I use the next trigger.
It updates every record

ALTER TRIGGER tr_Triooo_SM_ContactStructure on Triooo_SM_ContactStructure FOR UPDATE AS
IF UPDATE(item)
BEGIN
update Triooo_SM_ContactStructure set DateUpdate = GetDate()
END
GO
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-12-16 : 06:21:27
[code]ALTER TRIGGER tr_Triooo_SM_ContactStructure
ON Triooo_SM_ContactStructure
AFTER UPDATE
AS

IF UPDATE(item)
UPDATE x
SET x.DateUpdate = GetDate()
FROM Triooo_SM_ContactStructure AS x
INNER JOIN inserted as i on i.pkcol = x.pkcol[/code]


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -