首页 > 解决方案 > 使用单个插入语句插入 2 个表

问题描述

在 MSSQL 中,可以使用如下的单个插入语句(使用 OUTPUT 子句)插入 2 个表:

    insert into Table1(ID1 , Col1)
    OUTPUT inserted.ID1, Inserted.Col1
    into Table2
    values(1,'Col'), (2,'Col2');

在 postgresql 中我们有什么替代方案吗?我已经尝试过以下现在对我有用的方法:

     with cte as 
    (insert into Table1(ID1 , Col1)
    values(1,'Col'), (2,'Col2')
    returning *)
    insert into Table2 select * from cte ;

如果我们有其他选择,请告诉我。我也知道触发器,但我不想在这里使用它。

标签: sqlpostgresqlinsertsql-insert

解决方案


推荐阅读