Solved

Original @@identity value after insert trigger happens

Posted on 2008-10-09
3
690 Views
Last Modified: 2012-05-05
Hi,
I am trying to get the @@identity of a newly inserted row, after a insert happened.
The problem is that I also have an after insert tirgger on the table, that inserts to a log table, thereby updating @@identity. After the insert happens, I want the original ID of the insert, and not the id of the log table. Here is my trigger code:

alter TRIGGER MyTrigger
   ON  pmev
   AFTER INSERT
AS
BEGIN
      SET NOCOUNT ON;

      declare @PK1 int
      select @PK1 = PMEV_ID from inserted       
      insert into pmev2(PMEV_Descr1,PMEV_Descr2,PMEV_Descr3) values(getdate(),@PK1,3)

END
GO
0
Comment
Question by:wilcosw
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 22676600
use SCOPE_IDENTITY() instead of @@IDENTITY
0
 
LVL 1

Author Comment

by:wilcosw
ID: 22676634
Cool thanks,
here is also a link which explains it in more detail:
http://msdn.microsoft.com/en-us/library/aa259185.aspx
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22676643
correct
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question