SQL Server是否具有返回多个值的@@ IDENTITY?

时间:2015-02-06 22:10:47

标签: sql-server tsql

我知道当我在一个带有标识列的表中插入一行时,我可以使用SELECT @@IDENTITY检索刚刚插入的行的ID。

如果我执行一个插入语句,在同一个表中插入十行,是否有实用的方法来获取所有十行的ID?

2 个答案:

答案 0 :(得分:3)

是的,使用OUTPUT子句。无论如何,你应该将它作为首选。

更好的是,您可以使用OUTPUT返回多个字段。因此,您可以使用代理键和自然键来填充表变量。

答案 1 :(得分:0)

INSERT的OUTPUT子句缺乏:在插入后为源表中的每一行分配了Id是不可能的。

declare @Persistent table (Id int not null identity primary key, Value varchar(10));
declare @Temporary table (Id int not null primary key, Value varchar(10));

insert into @Temporary values (11, 'AAA'), (22, 'AAA'), (33, 'CCC');

insert into @Persistent (Value)
output inserted.Id
select Value from @Temporary;

在上面的例子中,我们将获得@Persistent表中实际ID的列表,但我们无法将这些ID映射到@Temporary表中的Ids,因为INSERT中的OUPTUT不允许获取源表字段 - 它只获取INSERTED表字段。

与INSERT相反,MERGE中的OUPTUT子句允许从源表中获取字段。因此,使用带有MERGE的OUTPUT可以解决问题:

merge into @Persistent as target
    using @Temporary as source on source.Id = target.Id        
when matched then
    update set Value = source.Value    
when not matched then
    insert (Value) values (Value)    
output 
    inserted.Id, source.Id;

以下是使用MERGE命令在父子表中插入 - 更新 - 删除行的示例。 MERGE OUTPUT的目标源映射集合非常有用:

-- definition of persistent tables

declare @Parents table (Id int not null identity primary key, Name varchar(10));
declare @Children table (Id int not null identity primary key, ParentId int, Name varchar(10));

-- imagine that persistent tables contain some data

insert into @Parents (Name) select 'Alfa';
insert into @Children (ParentId, Name) select 1, 'Delta';

-- definition of temporary tables

declare @TempParents table (Id int not null primary key, Name varchar(10));
declare @TempChildren table (Id int not null primary key, ParentId int, Name varchar(10));

-- data to insert (with negative Ids) and update (with real positive Ids)

insert into @TempParents values 
   (1, 'Alpha'), (-2, 'Bravo'), (-1, 'Charlie');

insert into @TempChildren values 
   (-9, 1, 'Alpha-1'),   (-8, 1, 'Alpha-2'),   (-7, 1, 'Alpha-2'),
   (-6, -2, 'Bravo-1'),   (-5, -2, 'Bravo-2'),   (-4, -2, 'Bravo-3'),
   (-3, -1, 'Charlie-1'), (-2, -1, 'Charlie-2'), (-1, -1, 'Charlie-3');

-- table to collect mapping Ids from @TempParents to @Parents

declare @ParentIdMaps table (ParentId int, TempParentId int)

-- merge data into @Parents table and collection of mapping Ids

merge into @Parents as target
using @TempParents as source on source.Id = target.Id    
when matched then
   update set   Name = source.Name
when not matched then
   insert (Name) values (Name)
output 
   inserted.Id, source.Id
into @ParentIdMaps
   (ParentId, TempParentId);

-- merge data into @Children table and use of mapping Ids

merge into @Children as target
using 
    (
       select
          Id,
          ParentId = m.ParentId,
          Name
       from
          @TempChildren tc
          inner join @ParentIdMaps m on m.TempParentId = tc.ParentId    
    )  
    as source on source.Id = target.Id    
when matched then
   update set ParentId = source.ParentId, Name = source.Name
when not matched then
   insert (ParentId, Name) values (ParentId, Name)
when not matched by source and target.ParentId in (select Id from @TempParents) then
   delete;

-- checking the result
-- see that 'Alfa' was renamed to 'Alpha' 
-- and 'Delta' was deleted because it was not mentioned in @TempChildren

select 
   p.*, 
   c.*
from      
   @Parents p
   inner join @Children c on c.ParentId = p.Id
order by
   p.Id,
   c.Id;