您可以使用该
output子句。从文档(重点是我的):
OUTPUT子句从受INSERT,UPDATe,DELETE或MERGE语句影响的每一行返回信息或基于表达式的信息。这些结果可以返回给处理应用程序,以用于诸如确认消息,归档和其他此类应用程序需求之类的事情。
结果也可以插入到表或表变量中。
此外,您可以在嵌套的INSERT,UPDATE,DELETE或MERGE语句中捕获OUTPUT子句的结果,然后将这些结果插入目标表或视图中。
像这样:
create table #tempids (a int) -- a temp table for holding our identity valuesinsert into #test (b,c) **output inserted.a into #tempids** -- put the inserted identity value into #tempidsvalues ('bvju','hjab')然后你问…
如果插入内容来自选择内容,该怎么办?
它的工作方式相同…
insert into #test (b,c) output inserted.a into #tempids -- put the inserted identity value into #tempids**select** -- except you use a select here**Column1,Column2from SomeSource**
无论是从值插入,派生表,execute语句,dml表源还是默认值,其工作方式都相同。
如果您插入1000条记录,则会在中获得1000个ID#tempids
。



