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
Solved

@@identity returning value of trigger insert not primary insert

Posted on 2012-03-26
6
516 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 69

Expert Comment

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

Accepted Solution

by:
Kent Olsen earned 167 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 167 total points
ID: 37766531
Try using Scope_Identity instead :

http://msdn.microsoft.com/en-us/library/ms190315.aspx
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 166 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Download ms sql express. 2 24
sql server query 12 23
SSMS Opening Mode 9 18
Requesting help with creating an SQL query with 2 tables 6 23
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

789 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