December 23, 2009 at 11:45 am
I have an insert procedure with @@identity set to an output variable. The output variable is bring back a number nowhere near the max id column. The max id column is somewhere near 91 and @@identity is giving me a number like 312. It is incrementing but not incrementing the right number. What could be causing this and is there anyway to reset this?
December 23, 2009 at 11:51 am
if there is a trigger on your destination table, @@identity could return the identity from that other table instead of the one you inserted; that's why you need to use the scope_identity() function isntead...it returns the identity related to the scope of your query.
if you have a very high transaction environment, @@identity could return the value of the last identity from the last table touched, instead of the one you are touching.
Lowell
December 23, 2009 at 12:51 pm
@@identity has a lot of weaknesses which could cause this. If you're using 2005 you should probably be looking at the OUTPUT clause to get back the ID you want, since it will return from the same table on the actual record you inserted. All o fthe other alternatives have weaknesses that could cuase it to return the wrong value in a multi-user, high concurrency setup.
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
December 23, 2009 at 1:31 pm
With SQL2005 and higher, simply use the OUTPUT clause to be 100% sure of the value assigned to your row(s).
There are a number of articles at SSC regarding the identity property.
Use the search feature.By default you will also get a ton of forum threads, but you can filter to receive only the articles.
Johan
Learn to play, play to learn !
Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere :w00t:
- How to post Performance Problems
- How to post data/code to get the best help[/url]
- How to prevent a sore throat after hours of presenting ppt
press F1 for solution, press shift+F1 for urgent solution 😀
Need a bit of Powershell? How about this
Who am I ? Sometimes this is me but most of the time this is me
December 23, 2009 at 3:48 pm
roundel1900 (12/23/2009)
I have an insert procedure with @@identity set to an output variable. The output variable is bring back a number nowhere near the max id column. The max id column is somewhere near 91 and @@identity is giving me a number like 312. It is incrementing but not incrementing the right number. What could be causing this and is there anyway to reset this?
For all the reasons others have stated, never use @@IDENTITY. Always use SCOPE_IDENTITY() instead. It will return the correct value whether you have triggers on the table or not. It works in all versions of SQL Server starting with 2000.
As a side bar... the OUTPUT clause works a fine trick but might be a little on the overkill side if you're you're just inserting one row at a time.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply