TSQL批量插入数据,同时将创建的id返回到原始表
我有两张桌子。一个称为@tempImportedData,另一个称为@tempEngine。 我在 @tempImportedData 中有数据,我想将此数据放入 @tempEngine,一旦插入 @tempEngine,就会创建一个 id。我希望将该 id 放回到相应行中的 @tempImportedData 中。我相信这就是 OUTPUT 语句的目的。我几乎有一个工作副本,请参见下文。
Declare @tempEngine as table(
id int identity(4,1) not null
,c1 int
,c2 int
);
Declare @tempImportedData as table(
c1 int
,c2 int
,engine_id int
);
insert into @tempImportedData (c1, c2)
select 1,1
union all select 1,2
union all select 1,3
union all select 1,4
union all select 2,1
union all select 2,2
union all select 2,3
union all select 2,4
;
INSERT INTO @tempEngine ( c1, c2 )
--OUTPUT INSERTED.c1, INSERTED.c2, INSERTED.id INTO @tempImportedData (c1, c2, engine_id) --dups with full data
--OUTPUT INSERTED.id INTO @tempImportedData (engine_id) -- new rows with wanted data, but nulls for rest
SELECT
c1
,c2
FROM
@tempImportedData
;
select * from @tempEngine ;
select * from @tempImportedData ;
我注释掉了以 OUTPUT 开头的两行。
第一个的问题是它将所有正确的数据插入到@tempImportedData中,因此最终结果是存在16行,前8行相同,但engine_id为空值,而第三列为空;其余 8 个已填充所有三列。最终结果应该有 8 行而不是 16 行。
第二个 OUTPUT 语句与第一个 OUTPUT 语句有相同的问题 - 16 行而不是 8 行。但是新的 8 行包含 null、null、engine_id
那么我如何更改此 TSQL 以获得 @tempImportedData .engine_id 更新而不插入新行?
I have two tables. One called @tempImportedData, another called @tempEngine.
I have data in @tempImportedData I would like to put this data into @tempEngine, once inserted into @tempEngine an id gets created. I would like that id to be placed back into @tempImportedData in the corresponding row. I believe this this the purpose of OUTPUT statement. I almost have a working copy please see below.
Declare @tempEngine as table(
id int identity(4,1) not null
,c1 int
,c2 int
);
Declare @tempImportedData as table(
c1 int
,c2 int
,engine_id int
);
insert into @tempImportedData (c1, c2)
select 1,1
union all select 1,2
union all select 1,3
union all select 1,4
union all select 2,1
union all select 2,2
union all select 2,3
union all select 2,4
;
INSERT INTO @tempEngine ( c1, c2 )
--OUTPUT INSERTED.c1, INSERTED.c2, INSERTED.id INTO @tempImportedData (c1, c2, engine_id) --dups with full data
--OUTPUT INSERTED.id INTO @tempImportedData (engine_id) -- new rows with wanted data, but nulls for rest
SELECT
c1
,c2
FROM
@tempImportedData
;
select * from @tempEngine ;
select * from @tempImportedData ;
I've commented out two lines starting with OUTPUT.
The problem with the first is that it inserts all of the correct data into @tempImportedData, so the end result is that 16 rows exist, the first 8 are the same with a null value for engine_id while the third column is null; the remaining 8 have all three columns populated. The end result should have 8 rows not 16.
The second OUTPUT statement has the same problem as the first - 16 rows instead of 8. However the new 8 rows contain null, null, engine_id
So how can I alter this TSQL to get @tempImportedData.engine_id updated without inserting new rows?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要另一个表变量 (
@temp
) 来捕获插入的输出,然后使用@temp
针对@tempImportedData
运行更新语句加入c1
和c2
。这要求c1
和c2
的组合在@tempImportedData
中是唯一的。You need another table variable (
@temp
) to capture the output from the insert and then run a update statement using the@temp
against@tempImportedData
joining onc1
andc2
. This requires that the combination ofc1
andc2
is unique in@tempImportedData
.@tempImportedData 中仍然保留着旧数据。第一个 OUTPUT 语句似乎在新行中插入了正确的数据,但旧行仍然存在。如果您对 @tempImportedData 运行 DELETE,删除脚本结尾处 engine_id 为 null 的所有行,则应该留下正确的八行。
@tempImportedData still has the old data still in it. The first OUTPUT statement seems to be inserting the right data in the new rows, but the old rows are still there. If you run a DELETE on @tempImportedData taking away all rows where engine_id is null at the end of your script, you should be left the correct eight rows.