2015-02-06 37 views
0

我知道,当我将一行插入到具有标识列的表中时,我可以使用SELECT @@IDENTITY来检索刚插入的行的标识。SQL Server是否具有返回多个值的@@ IDENTITY?

如果我做了一个插入10行到同一个表的插入语句,是否有一种实用的方法来获得所有10行的ID?

+0

也许是触发器? – 2015-02-06 22:12:19

+3

好的,如果你只有一条记录,那么你可以永远不要使用@@ identity ieven。如果有人将触发器插入带有标识的另一个表中,则可能返回错误的值。 – HLGEM 2015-02-06 22:12:24

+0

'OUTPUT'子句可以与'INSERT','UPDATE','DELETE'和'MERGE'一起使用,并在'UPDATE'的情况下提供对值和值之前的访问。 – HABO 2015-02-07 01:18:11

回答

3

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

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

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列表,但我们不能在@Temporary表的ID映射到IDS,因为OUPTUT在INSERT不允许获得源表中的字段 - 它只获取INSERTED表格字段。

与INSERT相反,MERGE中的OUPTUT子句允许从源表中获取字段。因此,使用与合并输出解决了这个问题:

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; 
相关问题