Sql server SCOPE_IDENTITY()多重插入

Sql server SCOPE_IDENTITY()多重插入,sql-server,sql-server-2008,tsql,sql-server-2005,Sql Server,Sql Server 2008,Tsql,Sql Server 2005,如何避免这些类型我可以为每个标识插入和在其他表中插入捕获值吗 Insert into LookupTables (a, b, c, d, e, OtherInfo, SortOrder) Values (a, b, c, d, e, NULL, NULL) DECLARE @LookupID INT = SCOPE_IDENTITY() Insert into LookupTables (a, b, c, d, e, OtherInfo, SortOrder) Values (a, b,

如何避免这些类型我可以为每个标识插入和在其他表中插入捕获值吗

Insert into LookupTables (a, b, c, d, e, OtherInfo, SortOrder)
Values (a, b, c, d, e, NULL, NULL)

DECLARE @LookupID INT = SCOPE_IDENTITY() 

Insert into LookupTables (a, b, c, d, e, OtherInfo, SortOrder)
Values (a, b, c, d, e, @LookupID, NULL)

Insert into LookupTables (a, b, c, d, e, OtherInfo, SortOrder)
Values (a, b, c, d, e, NULL, NULL)

DECLARE @LookupID2 INT = SCOPE_IDENTITY() 

Insert into LookupTables (a, b, c, d, e, OtherInfo, SortOrder)
Values (a, b, c, d, e, @LookupID2, NULL)

我想您是在问,如何在不为每个最后插入的标识值声明变量的情况下实现这一点。当然,实际上可以重用单个变量。但更容易的是完全跳过变量

Insert into LookupTables (a, b, c, d, e, OtherInfo, SortOrder)
Values (a, b, c, d, e, NULL, NULL)

Insert into LookupTables (a, b, c, d, e, OtherInfo, SortOrder)
Values (a, b, c, d, e, SCOPE_IDENTITY(), NULL)

Insert into LookupTables (a, b, c, d, e, OtherInfo, SortOrder)
Values (a, b, c, d, e, NULL, NULL)

Insert into LookupTables (a, b, c, d, e, OtherInfo, SortOrder)
Values (a, b, c, d, e, SCOPE_IDENTITY(), NULL)

我想您是在问,如何在不为每个最后插入的标识值声明变量的情况下实现这一点。当然,实际上可以重用单个变量。但更容易的是完全跳过变量

Insert into LookupTables (a, b, c, d, e, OtherInfo, SortOrder)
Values (a, b, c, d, e, NULL, NULL)

Insert into LookupTables (a, b, c, d, e, OtherInfo, SortOrder)
Values (a, b, c, d, e, SCOPE_IDENTITY(), NULL)

Insert into LookupTables (a, b, c, d, e, OtherInfo, SortOrder)
Values (a, b, c, d, e, NULL, NULL)

Insert into LookupTables (a, b, c, d, e, OtherInfo, SortOrder)
Values (a, b, c, d, e, SCOPE_IDENTITY(), NULL)
如果排序顺序是唯一的(我怀疑是唯一的)

然后插入值为()、()、()的all并使用子句

我确实这样做了,但如果排序顺序是唯一的(我怀疑是唯一的),我现在没有时间查找代码

declare @t table 
(
    id  int not null identity,
    a   int ,
    b   int ,
    c   int ,
    OtherInfo int
);


insert into @t (a, b, c)
output inserted.a, inserted.b, inserted.c, inserted.id 
into LookupTables (a, b, c, OtherInfo)
values (1,1,1),(2,2,2),(3,3,3),(4,4,4),(5,5,5);

select * from @t;
然后插入值为()、()、()的all并使用子句

我确实这样做了,但我现在没有时间查找代码

declare @t table 
(
    id  int not null identity,
    a   int ,
    b   int ,
    c   int ,
    OtherInfo int
);


insert into @t (a, b, c)
output inserted.a, inserted.b, inserted.c, inserted.id 
into LookupTables (a, b, c, OtherInfo)
values (1,1,1),(2,2,2),(3,3,3),(4,4,4),(5,5,5);

select * from @t;


我不明白这里的问题。问题是什么,或者你想做什么?您可以跳过声明变量,只将SCOPE_IDENTITY()作为列值。我从第一个语句中获取标识值,并将标识插入同一个表中的第二个语句。我不是每次都进行声明,以便插入多条记录。没有“魔力”方法-你正在做的是非常好的,这是一条路要走。只要你想继续使用“原始”的低级SQL,这就是最好的了。使用ORM(object relational mapper,对象关系映射器)可以极大地提高生产率,这也可以避免您现在需要手动编写大量枯燥、容易出错的“粘合”代码。要添加到Frisbee的答案中,可以使用子句从行中获取任何数据(注意复数形式),例如,新插入行的标识列值<代码>输出可与
插入
更新
删除
合并
一起使用,并在
更新
时提供对前后值的访问。你口袋里有一个很值得的工具。我不明白这里的问题。问题是什么,或者你想做什么?您可以跳过声明变量,只将SCOPE_IDENTITY()作为列值。我从第一个语句中获取标识值,并将标识插入同一个表中的第二个语句。我不是每次都进行声明,以便插入多条记录。没有“魔力”方法-你正在做的是非常好的,这是一条路要走。只要你想继续使用“原始”的低级SQL,这就是最好的了。使用ORM(object relational mapper,对象关系映射器)可以极大地提高生产率,这也可以避免您现在需要手动编写大量枯燥、容易出错的“粘合”代码。要添加到Frisbee的答案中,可以使用子句从行中获取任何数据(注意复数形式),例如,新插入行的标识列值<代码>输出可与
插入
更新
删除
合并
一起使用,并在
更新
时提供对前后值的访问。一个很值得放在你口袋里的工具。它能简单多少?你有4个独立的插页,对吗?不知道还能做些什么来让它“更简单”它能简单多少?你有4个独立的插页,对吗?不确定还能做些什么来“简化”