Solved

Original @@identity value after insert trigger happens

Posted on 2008-10-09
3
687 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 142

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 142

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

912 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now