如何检索通过insert ... select插入的行的标识? [英] How can I retrieve the identities of rows that were inserted through insert...select?

查看:68
本文介绍了如何检索通过insert ... select插入的行的标识?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过与此查询类似的查询插入记录:

I am inserting records through a query similar to this one:

insert into tbl_xyz select field1 from tbl_abc

现在,我想检索插入记录的新生成的IDENTITY值.如何以最小的锁定量和最大的可靠性做到这一点?

Now I would like to retreive the newly generated IDENTITY Values of the inserted records. How do I do this with minimum amount of locking and maximum reliability?

推荐答案

您可以使用输出子句.

您可以将信息输出到临时目标表或视图.

You can output your information to a temp target table or view.

这是一个例子:

DECLARE @InsertedIDs TABLE (ID bigint)
INSERT into DestTable (col1, col2, col3, col4)
OUTPUT INSERTED.ID INTO @InsertedIDs
SELECT col1, col2, col3, col4 FROM SourceTable

然后您可以在表InsertedIDs中查询所插入的ID.

You can then query the table InsertedIDs for your inserted IDs.

这篇关于如何检索通过insert ... select插入的行的标识?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆