使用CTE函数插入语句

时间:2018-09-27 13:31:28

标签: sql oracle plsql

我正在尝试这样做:

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函数?

1 个答案:

答案 0 :(得分:5)

尝试一下:

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)中失败。