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)
 Triggers: Insert and @@identity

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-07-29 : 21:59:36
Joel writes "I just came accross a frustrating problem that I understand, but I need a work around for.

I'm inserting into a table with a primary key field that increments itself
eg:
create table tPlacement
(placementNum int IDENTITY PRIMARY KEY, startDate datetime, endDate datetime)

So in a stored procedure I do something like this:

/* insert a row into tPlacement */
insert into tPlacement (startDate, endDate)
values (@startDate, @endDate)

/* get the index of the row you just inserted */
set @pk_index = @@identity

/* insert a row into the table that depends on tPlacement */
insert into tDepPlacement (placementNum, text)
values (@pk_index, 'information about placement')

Problem is this: when I added a trigger on an insert, the @@identity value is lost due to what ever lines of code are executed in the trigger.
Any ideas that don't require reworking a lot of code?"
   

- Advertisement -