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)
 Updte Trigger to access ID

Author  Topic 

kwikwisi
Constraint Violating Yak Guru

283 Posts

Posted - 2011-03-14 : 01:44:17
I'm creating a trigger after update that will check the status column of the record being updated and so on. The table has the Rec_ID column also.
How can I access the Rec_ID of record being updated ??

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-03-14 : 07:57:14
SET @REC_ID = (select rec_id from inserted)

Jim

Look at Books Online -- inserted and deleted are tables that exist and are accessible only in triggers.

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2011-03-14 : 08:03:14
No offence jim but this is a classic mistake :)

The inserted-table has/can have multiple rows like any other table and must be treated in a set based manner. You should use joins and standard set based methods instead:

select a.rec_id
from inserted a
inner join records b
on a.rec_id = b.rec_id
...


- Lumbago
My blog-> http://thefirstsql.com/2011/02/07/regular-expressions-advanced-string-matching-and-new-split-function-sql-server-2008-r2/
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-03-14 : 08:25:45
No offense taken at all, my sig line is sincere!

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page
   

- Advertisement -