Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Original @@identity value after insert trigger happens

Posted on 2008-10-09
3
Medium Priority
?
697 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1500 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

670 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