hi,
In SQL 2005, seems SCOPE_IDENTITY() will return the next available identity key.
This is different with SQL 2000, anybody knows why Microsoft change like this
Thanks
Bill
hi,
In SQL 2005, seems SCOPE_IDENTITY() will return the next available identity key.
This is different with SQL 2000, anybody knows why Microsoft change like this
Thanks
Bill
Seems SCOPE_IDENTITY() returns @@identity+1 in SQL 2005
Dirk Teufel
I hope I am not telling you to suck eggs here, but nore that SCOPE_IDENTITY() returns the last identity field created in the current scope, not the last identity field returned period. Other than that, I am not sure why you are getting that. Try comparing SCOPE_IDENTITY() with the value of @@IDENTITY. If SCOPE_IDENTITY() is one more than @@IDENTITY, then this is your problem.
HTH
doctus
Silly me. :p
Sorry I have posted the wrong function. I meant [IDENT_CURRENT].
I was hit by its value returned after we upgraded to SQL 2005. Here is the codes I have used on our production SQL server 2000 for 3 years.
Insert Members(DomainID, Email, Password, Created)
values(@DomainID, '', IDENT_CURRENT('Members')+1, getdate())
select @MemberID = SCOPE_IDENTITY()
Usually, the values in @MemberID and PASSWORD field are the same, but now
PASSWORD = @MemberID +1 in SQL 2005.
Bill
dzCepheus
RoyAF
Thanks Umachandar,
You are correct, I have tested on 3 three different servers with different versions.
My problem maybe caused by upgrading SQL server to 2005 as stated in BOL.
"Failed statements and transactions can change the current identity for a table and create gaps in the identity column values. The identity value is never rolled back even though the transaction that tried to insert the value into the table is not committed. For example, if an INSERT statement fails because of an IGNORE_DUP_KEY violation, the current identity value for the table is still incremented. "
Thanks again
Bill
qx
select ident_current('#ident')
insert into #ident default values
select ident_current('#ident')
insert into #ident default values
select ident_current('#ident')
drop table #ident
Nikko
Thanks for your response.
You can only get the identity key from SCOPE_IDENTITY or @@IDENTITY after a record is inserted into a table, but I need this key before I insert a record, and @@IDENTITY could return you a wrong ID if there is trigger in current table.
IDENT_CURRENT can return a last identity ID for a specific table.
My question is:
if IDENT_CURRENT returns NEXT last identity ID in SQL 2005, while it returns last identity ID in SQL 2000
Thanks
Bill