Search

Odd behaviour of SCOPE_IDENTITY with INSERT INTO ... OUTPUT ... INTO by Martin Smith

Closed
as Won't Fix Help for as Won't Fix

3
0
Sign in
to vote
Type: Bug
ID: 765809
Opened: 10/2/2012 3:43:21 PM
Access Restriction: Public
0
Workaround(s)
1
User(s) can reproduce this bug

Repro has three different queries of the following form

insert into #t
output inserted.a into #inserted_into
values ('a'),('b');
select scope_identity()

If only #t has an IDENTITY column defined then scope_identity returns the ID from #t.

If both tables have an IDENTITY column defined then scope_identity returns the ID from #inserted_into

If only #inserted_into has an IDENTITY column defined then scope_identity returns NULL.

(Originally found http://stackoverflow.com/q/12698388/73226)
Details (expand)

Product Language

English

Version

SQL Server 2012 - Developer Edition

Category

SQL Engine

Operating System

Not Applicable

Operating System Language

Not Applicable

Steps to Reproduce


set nocount on;

create table #t ( a char(1), i int identity (50, 1));
create table #inserted_into ( a char(1), i int identity (400,1));

insert into #t
output inserted.a into #inserted_into
values ('a'),('b');

select scope_identity(), 'Both have identity'

DROP TABLE #t, #inserted_into

go

create table #t ( a char(1));
create table #inserted_into ( a char(1), i int identity (400,1));

insert into #t
output inserted.a into #inserted_into
values ('a'),('b');

select scope_identity(), 'just #inserted_into has identity'

DROP TABLE #t, #inserted_into


go

create table #t ( a char(1), i int identity (50, 1));
create table #inserted_into ( a char(1));

insert into #t
output inserted.a into #inserted_into
values ('a'),('b');

select scope_identity(), 'just #t has identity'

DROP TABLE #t, #inserted_into

Actual Results


Returns


--------------------------------------- ------------------
401                                     Both have identity

                                        
--------------------------------------- --------------------------------
NULL                                    just #inserted_into has identity

                                        
--------------------------------------- --------------------
51                                     just #t has identity

Expected Results


BOL says

@@IDENTITY, SCOPE_IDENTITY, and IDENT_CURRENT return identity values generated only by the nested DML statement, and not those generated by the outer INSERT statement.

So I would have expected the first result to return 51

Platform

X64

Virtualization

 
File Attachments
0 attachments
Sign in to post a comment.
Posted by Microsoft on 2/27/2013 at 2:18 PM
Hello Martin,
We investigated the issue and found that changing the behavior is not an easy thing to do. It would basically require redefining some of the behavior when both INSERT & OUTPUT INTO target has identity columns.
Given the nature of the problem & the uncommon scenario, we have decided not to fix the issue.

--
Umachandar, SQL Programmability Team
Posted by Microsoft on 10/9/2012 at 11:54 AM
Hello Martin,
Thanks for reporting the issue. We will take a look at it and get back to you on our findings.

--
Umachandar, SQL Programmability Team
Sign in to post a workaround.