【问题标题】:Using CTE function for insert into statement使用 CTE 函数插入语句
【发布时间】:2019-03-03 10:37:53
【问题描述】:

我正在尝试这样做:

with
  function add_fnc(p_id number) return number
  is
  begin
    return p_id + 1; 
  end;
insert into temp_table
(
select add_fnc(1) from dual
);

但它显示编译错误:

ORA-00928:缺少 SELECT 关键字

有没有办法在插入语句中使用 CTE 函数?

【问题讨论】:

  • create or replace function add_fnc(p_id number) return number ... 然后insert into temp_table ...
  • 在尝试编写INSERT ... SELECT ... 语句时,不要将查询括在括号中。您在 VALUES 子句之后使用方括号,但您尝试使用替代形式。
  • @GordonLinoff。 Oracle 支持,但不支持插入语句
  • 您的 Oracle 版本是多少?

标签: sql oracle plsql


【解决方案1】:

试试这个:

create table demo (col1 int);

insert /*+ with_plsql */ into demo (col1)
with
    function add_one(p_id number) return number
    as
    begin
        return p_id + 1; 
    end;
select add_one(rownum) 
from dual
/

https://oracle-base.com/articles/12c/with-clause-enhancements-12cr1

某些工具可能无法处理这种语法。它在 SQL*Plus 12.1 中对我有用(当以斜杠字符终止时,对于 PL/SQL 代码)但在 PL/SQL Developer 12.0.7(Oracle 12.1)中失败。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 2016-06-20
    • 1970-01-01
    • 2013-02-11
    相关资源
    最近更新 更多