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 2000 Forums
 SQL Server Development (2000)
 Need help creating a trigger

Author  Topic 

dzirkelb
Yak Posting Veteran

53 Posts

Posted - 2010-10-07 : 10:48:01
I want to create a basic trigger that fires off when the field Appv is changed. When it is changed, I would like to update the field ApprovalDateTime with the current date/time.

I have found out how to run an update query when the update happens; however, I do not know how to identify what record specifically to update.

How can I tell the trigger what row to update? meaning, how do I find the primary key that was just updated from the trigger?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-10-07 : 11:15:24
create trigger dbo.trgMyTrigger
on dboMyTable
after update
as

set nocount on

if update(appv)
update t
set t.ApprovalDateTime = GETDATE()
from dbo.Mytable AS t
inner join inserted as i on i.PrimaryKeyColumnNameHere = t.PrimaryKeyColumnNameHere
where t.ApprovalDateTime is null
go



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

dzirkelb
Yak Posting Veteran

53 Posts

Posted - 2010-10-07 : 11:55:07
Worked like a charm, thanks!
Go to Top of Page
   

- Advertisement -