【发布时间】:2020-04-08 00:09:36
【问题描述】:
我遇到了一种情况
- 我需要将table1中的多行数据插入table2中
- 将每一行插入 table2,我需要获取范围 ID 并将更多数据插入 table3
我想出了类似下面的东西,但它似乎不断将相同的范围 ID 插入到 table3 中。
insert into dbo.table2(name_client, phone_client)
SELECT name_client, phone_client
from dbo.table1 where name_client is not null
declare @clientID INT = SCOPE_IDENTITY()
insert into dbo.table3(......, client_id)
SELECT ......., @clientID from table1 --where some condition
【问题讨论】:
-
@clientID只能保存一个值,它不能保存一个列表。而SCOPE_IDENTITY()只能获取单个值。我想你想要OUTPUT CLAUSE -
我没有从 table1 到 table3 的连接怎么可能? @DaleK
标签: sql sql-server