TSQL批量插入数据,同时将创建的id返回到原始表

发布于 2024-11-14 19:03:34 字数 1343 浏览 3 评论 0原文

我有两张桌子。一个称为@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我乃一代侩神 2024-11-21 19:03:34

您需要另一个表变量 (@temp) 来捕获插入的输出,然后使用 @temp 针对 @tempImportedData 运行更新语句加入 c1c2。这要求 c1c2 的组合在 @tempImportedData 中是唯一的。

Declare @temp as table(
     id int
    ,c1 int
    ,c2 int
);

INSERT INTO @tempEngine ( c1, c2 ) 
    OUTPUT INSERTED.id, INSERTED.c1, INSERTED.c2 INTO @temp
    SELECT 
         c1
        ,c2
    FROM 
        @tempImportedData
;       

UPDATE T1
  SET engine_id = T2.id
FROM @tempImportedData as T1
  INNER JOIN @temp as T2
    on T1.c1 = T2.c1 and
       T1.c2 = T2.c2
; 

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 on c1 and c2. This requires that the combination of c1 and c2 is unique in @tempImportedData.

Declare @temp as table(
     id int
    ,c1 int
    ,c2 int
);

INSERT INTO @tempEngine ( c1, c2 ) 
    OUTPUT INSERTED.id, INSERTED.c1, INSERTED.c2 INTO @temp
    SELECT 
         c1
        ,c2
    FROM 
        @tempImportedData
;       

UPDATE T1
  SET engine_id = T2.id
FROM @tempImportedData as T1
  INNER JOIN @temp as T2
    on T1.c1 = T2.c1 and
       T1.c2 = T2.c2
; 
萝莉病 2024-11-21 19:03:34

@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文