SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


What is the value of SCOPE_IDENTITY()


What is the value of SCOPE_IDENTITY()

Author
Message
sdorris-1150234
sdorris-1150234
Old Hand
Old Hand (353 reputation)Old Hand (353 reputation)Old Hand (353 reputation)Old Hand (353 reputation)Old Hand (353 reputation)Old Hand (353 reputation)Old Hand (353 reputation)Old Hand (353 reputation)

Group: General Forum Members
Points: 353 Visits: 433
Comments posted to this topic are about the item What is the value of SCOPE_IDENTITY()
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)

Group: General Forum Members
Points: 145434 Visits: 13350
Well, that was an easy one :-)
Good question to start the workweek.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Andrew Watson-478275
Andrew Watson-478275
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2917 Visits: 2863
A more interesting result is from this:


DECLARE @sid int

DECLARE @table Table (KeyID int primary key,KeyData varchar(5))
DECLARE @idtable Table (KeyID int identity primary key,KeyData varchar(5))

INSERT INTO @idtable (KeyData) VALUES('Test1')

INSERT INTO @table (KeyID,KeyData) VALUES(1,'Test1')
INSERT INTO @table (KeyID,KeyData) VALUES(2,'Test2')

SET @sid = SCOPE_IDENTITY()
select @sid



This still returns NULL, even though there has been an identity insert and the latest operations were on tables without identities.
Hugo Kornelis
Hugo Kornelis
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34303 Visits: 13112
Andrew Watson-478275 (4/19/2010)
A more interesting result is from this:


DECLARE @sid int

DECLARE @table Table (KeyID int primary key,KeyData varchar(5))
DECLARE @idtable Table (KeyID int identity primary key,KeyData varchar(5))

INSERT INTO @idtable (KeyData) VALUES('Test1')

INSERT INTO @table (KeyID,KeyData) VALUES(1,'Test1')
INSERT INTO @table (KeyID,KeyData) VALUES(2,'Test2')

SET @sid = SCOPE_IDENTITY()
select @sid



This still returns NULL, even though there has been an identity insert and the latest operations were on tables without identities.


Hi Andrew,

That is indeed very interesting. I checked Books Online, but it definitely says "last identity value inserted in the same scope", not "identity value inserted in last INSERT statement in the same scope", yet the latter is exactly what happpens. If you add SELECT SCOPE_IDENTITY(); directly after each INSERT statement, you'll see the correct result (1) after the first INSERT, but after the second it reverts to NULL.

I also searched on the connect site to see if this bug has already been reported, but that appears not to be the case. I recommend you to report this as a bug. If you then post the URL here, I'll vote on it (and probably some others too).


Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Tom Brown
Tom  Brown
SSCertifiable
SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)

Group: General Forum Members
Points: 6784 Visits: 1494
woo-hoo! - easy for a change
nigel.
nigel.
SSCertifiable
SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)

Group: General Forum Members
Points: 6466 Visits: 2985
Andrew Watson-478275 (4/19/2010)
A more interesting result is from this:


DECLARE @sid int

DECLARE @table Table (KeyID int primary key,KeyData varchar(5))
DECLARE @idtable Table (KeyID int identity primary key,KeyData varchar(5))

INSERT INTO @idtable (KeyData) VALUES('Test1')

INSERT INTO @table (KeyID,KeyData) VALUES(1,'Test1')
INSERT INTO @table (KeyID,KeyData) VALUES(2,'Test2')

SET @sid = SCOPE_IDENTITY()
select @sid



This still returns NULL, even though there has been an identity insert and the latest operations were on tables without identities.


I believe this is simply an issue with the documentation, as BOL is much clearer on the @@IDENTITY function, it says:

... If the statement did not affect any tables with identity columns, @@IDENTITY returns NULL. ...

I would expect the only significant difference between @@IDENTITY and SCOPE_IDENTITY to be their scope.

--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

Cadavre
Cadavre
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21932 Visits: 8519
After getting the last 4 wrong, it was nice to get one right for a change. . . Smile

yes, yes. I know. But I'm a .net coder, only recently decided to expand my knowledge to include some SQL


Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)

Group: General Forum Members
Points: 145434 Visits: 13350
nigel. (4/19/2010)
Andrew Watson-478275 (4/19/2010)
A more interesting result is from this:


DECLARE @sid int

DECLARE @table Table (KeyID int primary key,KeyData varchar(5))
DECLARE @idtable Table (KeyID int identity primary key,KeyData varchar(5))

INSERT INTO @idtable (KeyData) VALUES('Test1')

INSERT INTO @table (KeyID,KeyData) VALUES(1,'Test1')
INSERT INTO @table (KeyID,KeyData) VALUES(2,'Test2')

SET @sid = SCOPE_IDENTITY()
select @sid



This still returns NULL, even though there has been an identity insert and the latest operations were on tables without identities.



... If the statement did not affect any tables with identity columns, @@IDENTITY returns NULL. ...


But in the example there is a statement that affects a table with an identity constraint, so SCOPE_IDENTITY (or @@IDENTITY ) should have a value different from NULL, right?


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
nigel.
nigel.
SSCertifiable
SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)

Group: General Forum Members
Points: 6466 Visits: 2985
da-zero (4/19/2010)
nigel. (4/19/2010)
Andrew Watson-478275 (4/19/2010)
A more interesting result is from this:


DECLARE @sid int

DECLARE @table Table (KeyID int primary key,KeyData varchar(5))
DECLARE @idtable Table (KeyID int identity primary key,KeyData varchar(5))

INSERT INTO @idtable (KeyData) VALUES('Test1')

INSERT INTO @table (KeyID,KeyData) VALUES(1,'Test1')
INSERT INTO @table (KeyID,KeyData) VALUES(2,'Test2')

SET @sid = SCOPE_IDENTITY()
select @sid



This still returns NULL, even though there has been an identity insert and the latest operations were on tables without identities.



... If the statement did not affect any tables with identity columns, @@IDENTITY returns NULL. ...


But in the example there is a statement that affects a table with an identity constraint, so SCOPE_IDENTITY (or @@IDENTITY ) should have a value different from NULL, right?


I don't think so. The SCOPE_IDENTITY (or @@IDENTITY) function will return the value appropriate to the most recent INSERT statement whether that statement affects a table with an IDENTITY column or not

Maybe I should have included the preceding sentence from BOL which would have put it into context.

After an INSERT, SELECT INTO, or bulk copy statement is completed, @@IDENTITY contains the last identity value that is generated by the statement. If the statement did not affect any tables with identity columns, @@IDENTITY returns NULL.

--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)

Group: General Forum Members
Points: 145434 Visits: 13350
nigel. (4/19/2010)


/* lots of quoting here */

I don't think so. The SCOPE_IDENTITY (or @@IDENTITY) function will return the value appropriate to the most recent INSERT statement whether that statement affects a table with an IDENTITY column or not

Maybe I should have included the preceding sentence from BOL which would have put it into context.

After an INSERT, SELECT INTO, or bulk copy statement is completed, @@IDENTITY contains the last identity value that is generated by the statement. If the statement did not affect any tables with identity columns, @@IDENTITY returns NULL.


BOL on SCOPE_IDENTITY:

Returns the last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch. Therefore, two statements are in the same scope if they are in the same stored procedure, function, or batch.

In the example, since there are no GO statements to define batch boundaries, aren't all the statements in the same batch?


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search