【问题标题】:Oracle returning statement for an insert into operation with 'select from' sourceOracle 返回语句以使用“select from”源进行插入操作
【发布时间】:2019-02-12 03:02:41
【问题描述】:

我们希望返回从插入语句插入的 id,该语句以“select from”子句作为其值的来源。

例子:

Create table Table1 (Col1 Number)

declare
  vId number;
begin
    insert into Table1 (select 1 from dual) 
    returning COL1 into vId;
end;

错误:

ORA-06550:第 5 行,第 5 列:
PL/SQL: ORA-00933: SQL 命令未正确结束

语法中是否缺少某些内容,是否可以这样做?谢谢。

【问题讨论】:

  • returning into 不适用于INSERT INTO SELECT * 。您应该为此使用 FORALL 块。见thisthis

标签: oracle plsql


【解决方案1】:

仅当列列在 VALUES 之前时才返回作品:

declare
  vId number;
begin
    insert into Table1 (col1) VALUES ((select 1 from dual)) 
    returning COL1 into vId;
    dbms_output.put_line( vId);
end;


==========
vId: 1

【讨论】:

  • 感谢@q4za4,这适用于单行插入,但不适用于多行。正如 Kaushik Nayak 建议的那样,使用 forall 语句是有效的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-11
  • 1970-01-01
  • 2014-05-01
相关资源
最近更新 更多