{"id":21,"date":"2008-09-19T00:58:38","date_gmt":"2008-09-19T07:58:38","guid":{"rendered":"http:\/\/myplace.bluecastle.us\/2008\/9?p=21"},"modified":"2008-09-19T00:58:38","modified_gmt":"2008-09-19T07:58:38","slug":"scope_identity-returns-a-value","status":"publish","type":"post","link":"http:\/\/myplace.bluecastle.us\/?p=21","title":{"rendered":"SCOPE_IDENTITY() returns a value?"},"content":{"rendered":"<p>I have long used SCOPE_IDENTITY() to get the primary key of a newly inserted row immediately after the INSERT statement. For example: <\/p>\n<p><code>INSERT INTO myTable<br \/>SELET @var = SCOPE_IDENTITY() <\/code><\/p>\n<p>What I incorrectly assumed was that the value was reset with each statement similar to the behavior of @@ERROR. If not each statement, then each INSERT statement. In other words, [@more@]if I insert zero records then I expected SCOPE_IDENTITY to return a null value. In actuallity, both SCOPE_IDENTITY and @@IDENTITY retain thier values until another identity value is generated. <\/p>\n<p>As SQL Server Books Online says: <\/p>\n<p><em>SCOPE_IDENTITY and @@IDENTITY return the last identity values that are generated in any table in the current session. However, SCOPE_IDENTITY returns values inserted only within the current scope; @@IDENTITY is not limited to a specific scope.<\/em> <a href=\"http:\/\/bluecastle.us\/Which%20returns%20this:%20%28look%20closely%20at%20the%20%27after%202cnd%20insert%27%29\" target=\"_blank\">weblink <\/a><\/p>\n<p>Run the following code against a server to see. (Should work against any 2000 or 2005 server.)<\/p>\n<p><span style=\"font-size: 10pt; font-family: 'Courier New'; color: #008000\">&#8212; ============================================<\/span><br \/><span style=\"font-size: 10pt; font-family: 'Courier New'; color: #008000\">&#8212; TEST<\/span><br \/><span style=\"font-size: 10pt; font-family: 'Courier New'; color: #008000\">&nbsp;<\/span><br \/><span style=\"font-size: 10pt; font-family: 'Courier New'; color: #008000\">&#8212; select identities<\/span><br \/><span style=\"font-size: 10pt; font-family: 'Courier New'\"><span>&nbsp;&nbsp;&nbsp; <\/span><br \/><span style=\"color: #0000ff\">SELECT<\/span> <span style=\"color: #ff0000\">&#39;Before first insert:&#39;<\/span><span style=\"color: #808080\">,<\/span> <span style=\"color: #ff00ff\">@@IDENTITY<\/span> <span style=\"color: #ff0000\">&#39;@@IDENTITY&#39;<\/span><span style=\"color: #808080\">,<\/span> <span style=\"color: #ff00ff\">SCOPE_IDENTITY<\/span><span style=\"color: #808080\">()<\/span><span style=\"color: #ff0000\">&#39;SCOPE_IDENTITY()&#39;<\/span><span style=\"color: #808080\">,<\/span><span style=\"color: #ff00ff\">@@ROWCOUNT<\/span> <span style=\"color: #ff0000\">&#39;@@ROWCOUNT&#39;<\/span><\/span><br \/><span style=\"font-size: 10pt; font-family: 'Courier New'; color: #ff0000\">&nbsp;<\/span><br \/><span style=\"font-size: 10pt; font-family: 'Courier New'; color: #008000\">&#8212; Insert 10 records into table<\/span><span style=\"font-size: 10pt; font-family: 'Courier New'\"><br \/><span style=\"color: #0000ff\">INSERT<\/span> <span style=\"color: #0000ff\">INTO<\/span> @tblTemp01&nbsp;<\/span><span style=\"font-size: 10pt; font-family: 'Courier New'\"><br \/><span style=\"color: #0000ff\">SELECT<\/span> <span style=\"color: #0000ff\">TOP<\/span> 10 SPECIFIC_NAME&nbsp;<\/span><span style=\"font-size: 10pt; font-family: 'Courier New'\"><br \/><span style=\"color: #0000ff\">FROM<\/span> <span style=\"color: #008000\">INFORMATION_SCHEMA.ROUTINES<\/span> <\/span><br \/><span style=\"font-size: 10pt; font-family: 'Courier New'\">&nbsp;<\/span><br \/><span style=\"font-size: 10pt; font-family: 'Courier New'; color: #008000\">&#8212; select identities<\/span><span style=\"font-size: 10pt; font-family: 'Courier New'\"><br \/><span style=\"color: #0000ff\">SELECT<\/span> <span style=\"color: #ff0000\">&#39;After first insert:&#39;<\/span><span style=\"color: #808080\">,<\/span> <span style=\"color: #ff00ff\">@@IDENTITY<\/span> <span style=\"color: #ff0000\">&#39;@@IDENTITY&#39;<\/span><span style=\"color: #808080\">,<\/span> <span style=\"color: #ff00ff\">SCOPE_IDENTITY<\/span><span style=\"color: #808080\">()<\/span><span style=\"color: #ff0000\">&#39;SCOPE_IDENTITY()&#39;<\/span><span style=\"color: #808080\">,<\/span><span style=\"color: #ff00ff\">@@ROWCOUNT<\/span> <span style=\"color: #ff0000\">&#39;@@ROWCOUNT&#39;<\/span><\/span><br \/><span style=\"font-size: 10pt; font-family: 'Courier New'; color: #ff0000\">&nbsp;<\/span><br \/><span style=\"font-size: 10pt; font-family: 'Courier New'; color: #008000\">&#8212; Insert 0 records into table<\/span><span style=\"font-size: 10pt; font-family: 'Courier New'\"><br \/><span style=\"color: #0000ff\">INSERT<\/span> <span style=\"color: #0000ff\">INTO<\/span> @tblTemp01<\/span><span style=\"font-size: 10pt; font-family: 'Courier New'\"><br \/><span style=\"color: #0000ff\">SELECT<\/span> <span style=\"color: #0000ff\">TOP<\/span> 10 SPECIFIC_NAME&nbsp;<\/span><span style=\"font-size: 10pt; font-family: 'Courier New'\"><br \/><span style=\"color: #0000ff\">FROM<\/span> <span style=\"color: #008000\">INFORMATION_SCHEMA.ROUTINES<\/span>&nbsp;<\/span><span style=\"font-size: 10pt; font-family: 'Courier New'\"><br \/><span style=\"color: #0000ff\">WHERE<\/span> SPECIFIC_NAME <span style=\"color: #808080\">=<\/span> <span style=\"color: #ff0000\">&#39;A_very_unlikely_name_to_RETURn_0_rows&#39;<\/span><\/span><br \/><span style=\"font-size: 10pt; font-family: 'Courier New'; color: #ff0000\">&nbsp;<\/span><br \/><span style=\"font-size: 10pt; font-family: 'Courier New'; color: #008000\">&#8212; select identities<\/span><span style=\"font-size: 10pt; font-family: 'Courier New'\"><br \/><span style=\"color: #0000ff\">SELECT<\/span> <span style=\"color: #ff0000\">&#39;After second insert:&#39;<\/span><span style=\"color: #808080\">,<\/span> <span style=\"color: #ff00ff\">@@IDENTITY<\/span> <span style=\"color: #ff0000\">&#39;@@IDENTITY&#39;<\/span><span style=\"color: #808080\">,<\/span> <span style=\"color: #ff00ff\">SCOPE_IDENTITY<\/span><span style=\"color: #808080\">()<\/span><span style=\"color: #ff0000\">&#39;SCOPE_IDENTITY()&#39;<\/span><span style=\"color: #808080\">,<\/span><span style=\"color: #ff00ff\">@@ROWCOUNT<\/span> <span style=\"color: #ff0000\">&#39;@@ROWCOUNT&#39;<\/span><\/span><br \/><span style=\"font-size: 10pt; font-family: 'Courier New'; color: #ff0000\">&nbsp;<\/span><br \/><span style=\"font-size: 10pt; font-family: 'Courier New'; color: #008000\">&#8212; select from table<\/span><span style=\"font-size: 10pt; font-family: 'Courier New'\"><br \/><span style=\"color: #0000ff\">SELECT<\/span> <span style=\"color: #808080\">*<\/span> <span style=\"color: #0000ff\">FROM<\/span> @tblTemp01<\/span> <\/p>\n<p>&nbsp;<\/p>\n<p>aldkjfkljasd;lkfj a;ldkjf&nbsp; a;lkdsfj<\/p>\n<div>Which returns this: (look closely at the &#39;after 2cnd insert&#39;)<\/div>\n<div>&nbsp;<\/div>\n<div>&nbsp;<img decoding=\"async\" src=\"http:\/\/myplace.bluecastle.us\/gallery\/1\/Screenshot%20-%209_18_2008%20%2C%202_39_20%20PM.png\" border=\"0\" \/><\/div>\n<div>&nbsp;<\/div>\n<div>&nbsp;<\/div>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">I have long used SCOPE_IDENTITY() to get the primary key of a newly inserted row immediately after the INSERT statement. For example: INSERT INTO myTableSELET @var = SCOPE_IDENTITY() What I incorrectly assumed was that the value was reset with each statement similar to the behavior of @@ERROR. If not each statement, then each INSERT statement. In other words, [@more@]if I&hellip; <a href=\"http:\/\/myplace.bluecastle.us\/?p=21\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,8,9],"tags":[],"class_list":["post-21","post","type-post","status-publish","format-standard","hentry","category-code","category-software","category-sql","xfolkentry"],"_links":{"self":[{"href":"http:\/\/myplace.bluecastle.us\/index.php?rest_route=\/wp\/v2\/posts\/21","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/myplace.bluecastle.us\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/myplace.bluecastle.us\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/myplace.bluecastle.us\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/myplace.bluecastle.us\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=21"}],"version-history":[{"count":0,"href":"http:\/\/myplace.bluecastle.us\/index.php?rest_route=\/wp\/v2\/posts\/21\/revisions"}],"wp:attachment":[{"href":"http:\/\/myplace.bluecastle.us\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/myplace.bluecastle.us\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/myplace.bluecastle.us\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}