Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

@@identity returning value of trigger insert not primary insert

Posted on 2012-03-26
6
Medium Priority
?
535 Views
Last Modified: 2012-08-13
Hi,

I have an insert statrment in my sp followed directly by:
set @ClientCaseID = @@identity

@ClientCaseID is then returned.

It was all working fine.

I then needed to audit things so added a trigger that created a record in a historical table on an insert into the table above.

The problem now is that @ClientCaseID is the value of the insert occuring in the trigger, not my insert statement above as before.

How can I get around this?
0
Comment
Question by:scm0sml
6 Comments
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 37766525
Use IDENTITY_SCOPE or ()SCOPE_IDENTITY)
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 668 total points
ID: 37766527
Yep.  That's what @@IDENTITY does -- returns that the identity value of the most recent insert.

Try using scope_identity.  It returns the identity value in the current scope (which would be the INSERT statement in your procedure, not the trigger).


Good Luck,
Kent
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 668 total points
ID: 37766531
Try using Scope_Identity instead :

http://msdn.microsoft.com/en-us/library/ms190315.aspx
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 664 total points
ID: 37766534
Yes, it's SCOPE_IDENTITY(), I couldn't remember for sure which way on the first post :-) .
0
 
LVL 13

Expert Comment

by:Ashok
ID: 37766543
Try

set @ClientCaseID =  IDENT_CURRENT('tablename')

change tablename to the correct tablename.

HTH
Ashok
0
 

Author Comment

by:scm0sml
ID: 37766628
Ah ha I've learned something new there!!

Thanks all.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

972 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