Solved

Original @@identity value after insert trigger happens

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert in derived column 7 30
query execution hang 5 31
SQL Error - Query 6 26
Need help constructing a conditional update query 16 47
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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 extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

777 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