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 2008 Forums
 Transact-SQL (2008)
 Getting Trigger info

Author  Topic 

Steve2106
Posting Yak Master

183 Posts

Posted - 2011-08-10 : 06:24:25
Hi There,

I have an update trigger and I need to return the Id of the record that caused the trigger. How would I get that.

IE I update a record with an ID of 15 which fires a trigger.
When the trigger runs I need it to give me the record ID of 15 so I can run something else from the trigger.

Hope that makes sense.

Best Regards,



Steve

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-08-10 : 07:01:33
Assuming that ID is one of the columns in the table that you updated which caused the trigger to fire, the virtual tables INSERTED and/or DELETED would have the information. http://msdn.microsoft.com/en-us/library/ms191300.aspx
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2011-08-10 : 08:09:47
In short; put the following inside the trigger

SELECT ID FROM inserted

Just be aware that of you update multiple rows at once this inserted virtual table will hold ALL the rows. It is in other words a bad idea to do something like this SET ID = (SELECT ID FROM inserted) because it will potentially try to insert several ID's in to the same variable and this is not possible.

- Lumbago
My blog-> http://thefirstsql.com/2011/07/08/how-to-find-gaps-in-identity-columns-at-the-speed-of-light/
Go to Top of Page
   

- Advertisement -