Solved

@@identity returning value of trigger insert not primary insert

Posted on 2012-03-26
6
507 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:
Kdo 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
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.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS 2013 - Creating a summarized report 19 32
T-SQL: Do I need CLUSTERED here? 13 41
sql server tables from access 18 17
Can > be used for a Text field 6 37
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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

785 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