﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by sdorris-1150234  / What is the value of SCOPE_IDENTITY() / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 23 May 2013 02:01:59 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>nice question</description><pubDate>Thu, 10 Jun 2010 01:52:59 GMT</pubDate><dc:creator>vignesh 38804</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>[quote][b]Hugo Kornelis (4/19/2010)[/b][hr]Tough question. We can't really look in the minds of the developers who originally wrote the feature, so we can only speculate.[/quote]Very true[quote]And frankly, I see little reason to do an INSERT with an IDENTITY, and then first do another INSERT before testing SCOPE_IDENTITY, ...[/quote]Yes, it is a strange thing to do in the first place.[quote]Given the similarlty to @@IDENTITY pointed out elsewhere in this discussion, that might very well even be the originally intended behaviour.[/quote]Agreed, given that the documentation for @@IDENTITY is quite clear on this it's odd (at the very least) that it's not covered in the SCOPE_IDENTITY docs.</description><pubDate>Tue, 20 Apr 2010 01:59:49 GMT</pubDate><dc:creator>nigel.</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>Given the number of inconsistencies and bugs, I'd be quite happy to see @@IDENTITY and SCOPE_IDENTITY removed from the product.  I very much prefer the OUTPUT clause for this purpose.[url]http://connect.microsoft.com/SQLServer/feedback/details/328811/scope-identity-sometimes-returns-incorrect-value[/url]</description><pubDate>Mon, 19 Apr 2010 22:20:53 GMT</pubDate><dc:creator>Paul White</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>You're welcome! It gave me a headache one day trying to debug an application, so I thought I'd post the question.I've learned a great deal from this forum. It's amazing to me how even a simple question can generate a great discussion. That's why I keep coming back.</description><pubDate>Mon, 19 Apr 2010 15:49:34 GMT</pubDate><dc:creator>sdorris-1150234</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>An excellent question! - Thank you!</description><pubDate>Mon, 19 Apr 2010 15:33:42 GMT</pubDate><dc:creator>Clive Chinery</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>[quote][b]nigel. (4/19/2010)[/b][hr]Hugo,Thanks for the input. Which would you say is correct, the implementation as it stands or BOL?[/quote]Hi Nigel,Tough question. We can't really look in the minds of the developers who originally wrote the feature, so we can only speculate.And frankly, I see little reason to do an INSERT with an IDENTITY, and then first do another INSERT before testing SCOPE_IDENTITY, so I really would not mind much if Microsoft decides to change the docs, not the implementation. Given the similarlty to @@IDENTITY pointed out elsewhere in this discussion, that might very well even be the originally intended behaviour.</description><pubDate>Mon, 19 Apr 2010 11:04:21 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>Thanks for the Question</description><pubDate>Mon, 19 Apr 2010 09:49:54 GMT</pubDate><dc:creator>SQLRNNR</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>I just tried it in SQL2000 (my original attempt was in 2008), and it does the same, so I'd guess this is what is meant to happen, and it's probably BOL at fault (or it's a really old bug).</description><pubDate>Mon, 19 Apr 2010 09:17:17 GMT</pubDate><dc:creator>Andrew Watson-478275</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>Hugo,Thanks for the input. Which would you say is correct, the implementation as it stands or BOL?</description><pubDate>Mon, 19 Apr 2010 09:12:25 GMT</pubDate><dc:creator>nigel.</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>[quote][b]nigel. (4/19/2010)[/b][hr]This is how I interpret the docs:SCOPE_IDENTITY will return the identity value from the most recent insert in the current scope. If the most recent insert was to a table that has no IDENTITY column then the value returned by SCOPE_IDENTITY will be NULL.[/quote]Hi Nigel,While that is, obviously, exactly what SCOPE_IDENTITY does, it is absolutely NOT what Books Online says, and there is no way you can interpret the text in Books Online that way.This is exactly why I recommended Andrew to file this as a bug on the Connect site. Microsoft can then decide whether this is a documentation bug or a product bug, and fix either the docs or the product.</description><pubDate>Mon, 19 Apr 2010 07:34:24 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>We had a .Net application that kept getting a null reference returned from a stored procedure. Since SCOPE_IDENTITY() was used after the INSERT, we assumed the value was OK until more in-depth debugging pointed to the primary key.Hopefully, even the simple questions serve a purpose. I'm certainly more cautious about making assumptions while debugging problems.</description><pubDate>Mon, 19 Apr 2010 07:24:39 GMT</pubDate><dc:creator>sdorris-1150234</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>I was so happy to get a question right for the first time in 5 tries - even if I did realize it was relatively easy once I looked closely at the SCOPE_IDENTITY function name. :-)This is a good basic question and a good starting point for further learning about SQL variable scope.Thanks,webrunner</description><pubDate>Mon, 19 Apr 2010 07:15:53 GMT</pubDate><dc:creator>webrunner</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>[quote][b]da-zero (4/19/2010)[/b][hr]I interprete it as:SCOPE_IDENTITY returns the value of the last identity value inserted in the same scope. Since the scope is the whole batch, it doesn't matter if the identity inserted was in the last statement or not.This statement inserts an identity value and it belongs to the same batch, so SCOPE_IDENTITY shouldn't return NULL:[code="sql"]INSERT INTO @idtable (KeyData) VALUES('Test1')[/code][/quote]Hmm, looks like we need another opinion, :-)</description><pubDate>Mon, 19 Apr 2010 06:57:05 GMT</pubDate><dc:creator>nigel.</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>I interprete it as:SCOPE_IDENTITY returns the value of the last identity value inserted in the same scope. Since the scope is the whole batch, it doesn't matter if the identity inserted was in the last statement or not.This statement inserts an identity value and it belongs to the same batch, so SCOPE_IDENTITY shouldn't return NULL:[code="sql"]INSERT INTO @idtable (KeyData) VALUES('Test1')[/code]</description><pubDate>Mon, 19 Apr 2010 06:22:43 GMT</pubDate><dc:creator>Koen Verbeeck</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>[quote][b]da-zero (4/19/2010)[/b][hr][quote][b]nigel. (4/19/2010)[/b][hr]In the example, since there are no GO statements to define batch boundaries, aren't all the statements in the same batch?[/quote]They are yes, but I'm not sure how this is relevant to the issue of why SCOPE_IDENTITY returns NULL in this scenario?This is how I interpret the docs:SCOPE_IDENTITY will return the identity value from the most recent insert in the current scope. If the most recent insert was to a table that has no IDENTITY column then the value returned by SCOPE_IDENTITY will be NULL. </description><pubDate>Mon, 19 Apr 2010 06:06:03 GMT</pubDate><dc:creator>nigel.</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>[quote][b]nigel. (4/19/2010)[/b][hr]/* 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 notMaybe I should have included the preceding sentence from BOL which would have put it into context.[b][i]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.[/i][/b][/quote]BOL on SCOPE_IDENTITY:[b][i]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.[/i][/b]In the example, since there are no GO statements to define batch boundaries, aren't all the statements in the same batch?</description><pubDate>Mon, 19 Apr 2010 05:29:10 GMT</pubDate><dc:creator>Koen Verbeeck</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>[quote][b]da-zero (4/19/2010)[/b][hr][quote][b]nigel. (4/19/2010)[/b][hr][quote][b]Andrew Watson-478275 (4/19/2010)[/b][hr]A more interesting result is from this:[code]DECLARE @sid intDECLARE @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[/code]This still returns NULL, even though there has been an identity insert and the latest operations were on tables without identities.[/quote][b][i]... If the statement did not affect any tables with identity columns, @@IDENTITY returns NULL. ...[/i][/b][/quote]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?[/quote]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 notMaybe I should have included the preceding sentence from BOL which would have put it into context.[b][i]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.[/i][/b]</description><pubDate>Mon, 19 Apr 2010 05:04:20 GMT</pubDate><dc:creator>nigel.</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>[quote][b]nigel. (4/19/2010)[/b][hr][quote][b]Andrew Watson-478275 (4/19/2010)[/b][hr]A more interesting result is from this:[code]DECLARE @sid intDECLARE @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[/code]This still returns NULL, even though there has been an identity insert and the latest operations were on tables without identities.[/quote][b][i]... If the statement did not affect any tables with identity columns, @@IDENTITY returns NULL. ...[/i][/b][/quote]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?</description><pubDate>Mon, 19 Apr 2010 04:30:14 GMT</pubDate><dc:creator>Koen Verbeeck</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>After getting the last 4 wrong, it was nice to get one right for a change. . . :)[size="2"]yes, yes. I know. But I'm a .net coder, only recently decided to expand my knowledge to include some SQL[/size]</description><pubDate>Mon, 19 Apr 2010 04:17:43 GMT</pubDate><dc:creator>Cadavre</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>[quote][b]Andrew Watson-478275 (4/19/2010)[/b][hr]A more interesting result is from this:[code]DECLARE @sid intDECLARE @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[/code]This still returns NULL, even though there has been an identity insert and the latest operations were on tables without identities.[/quote]I believe this is simply an issue with the documentation, as BOL is much clearer on the @@IDENTITY function, it says:[b][i]... If the statement did not affect any tables with identity columns, @@IDENTITY returns NULL. ...[/i][/b]I would expect the only significant difference between @@IDENTITY and SCOPE_IDENTITY to be their scope.</description><pubDate>Mon, 19 Apr 2010 04:15:46 GMT</pubDate><dc:creator>nigel.</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>woo-hoo! - easy for a change</description><pubDate>Mon, 19 Apr 2010 03:21:15 GMT</pubDate><dc:creator>Tom  Brown</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>[quote][b]Andrew Watson-478275 (4/19/2010)[/b][hr]A more interesting result is from this:[code]DECLARE @sid intDECLARE @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[/code]This still returns NULL, even though there has been an identity insert and the latest operations were on tables without identities.[/quote]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).</description><pubDate>Mon, 19 Apr 2010 03:01:31 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>A more interesting result is from this:[code]DECLARE @sid intDECLARE @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[/code]This still returns NULL, even though there has been an identity insert and the latest operations were on tables without identities.</description><pubDate>Mon, 19 Apr 2010 02:40:21 GMT</pubDate><dc:creator>Andrew Watson-478275</dc:creator></item><item><title>RE: What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>Well, that was an easy one :-)Good question to start the workweek.</description><pubDate>Mon, 19 Apr 2010 02:13:21 GMT</pubDate><dc:creator>Koen Verbeeck</dc:creator></item><item><title>What is the value of SCOPE_IDENTITY()</title><link>http://www.sqlservercentral.com/Forums/Topic905517-2677-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/questions/T-SQL/69800/"&gt;What is the value of SCOPE_IDENTITY()&lt;/A&gt;[/B]</description><pubDate>Sat, 17 Apr 2010 20:14:39 GMT</pubDate><dc:creator>sdorris-1150234</dc:creator></item></channel></rss>