Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

What is the value of SCOPE_IDENTITY() Expand / Collapse
Author
Message
Posted Saturday, April 17, 2010 8:14 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, July 22, 2011 7:33 AM
Points: 289, Visits: 433
Comments posted to this topic are about the item What is the value of SCOPE_IDENTITY()
Post #905517
Posted Monday, April 19, 2010 2:13 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:03 PM
Points: 13,606, Visits: 10,489
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #905758
Posted Monday, April 19, 2010 2:40 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 5:42 AM
Points: 1,216, Visits: 2,208
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.
Post #905770
Posted Monday, April 19, 2010 3:01 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 4:11 PM
Points: 5,969, Visits: 8,228
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 MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #905783
Posted Monday, April 19, 2010 3:21 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Sunday, August 3, 2014 3:41 PM
Points: 1,293, Visits: 1,430
woo-hoo! - easy for a change
Post #905789
Posted Monday, April 19, 2010 4:15 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 2:18 AM
Points: 1,179, Visits: 2,650
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

Post #905812
Posted Monday, April 19, 2010 4:17 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 8:49 AM
Points: 2,434, Visits: 7,506
After getting the last 4 wrong, it was nice to get one right for a change. . . :)

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



Not a DBA, just 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


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #905814
Posted Monday, April 19, 2010 4:30 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:03 PM
Points: 13,606, Visits: 10,489
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #905820
Posted Monday, April 19, 2010 5:04 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 2:18 AM
Points: 1,179, Visits: 2,650
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

Post #905835
Posted Monday, April 19, 2010 5:29 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:03 PM
Points: 13,606, Visits: 10,489
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #905847
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse