创建序列时PL / SQL Oracle脚本中的语法错误

时间:2016-09-05 08:32:34

标签: oracle plsql

我有以下PL / SQL脚本,我试图使用Oracle SQL Developer的“运行脚本”命令执行该脚本。

declare      
  current_max_value_id      number(8, 0);

begin

  select max(id) into current_max_value_id from "DIM_VALUE";

  create sequence value_id_seq 
    increment by 1
    start with current_max_value_id + 1
    nomaxvalue
    nocycle; 

  insert all                
    into "DIM_VALUE" (ID, UNIT_ID) values (value_id_seq.NEXTVAL, 1)
    into "DIM_VALUE" (ID, UNIT_ID) values (value_id_seq.NEXTVAL, 2)
    ...
  select * from dual;

  drop sequence value_id_seq;

end;

我收到以下错误:

PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:

在搜索类似的问题之后,我想这应该以某种方式解决将/放在某处,但我还没有弄清楚究竟在哪里(以及为什么它甚至需要)不是脚本,如上所述,对口译员来说毫不含糊?)。

1 个答案:

答案 0 :(得分:4)

您不能在PL / SQL上使用DDL。您需要execute immediate

declare      
  current_max_value_id      number(8, 0);

begin

  select max(id) into current_max_value_id from DIM_VALUE;

  execute immediate 'create sequence value_id_seq increment by 1 start with current_max_value_id + 1 nomaxvalue nocycle'; 

  -- Edited based on Alex's Poole comment
  execute immediate 'insert into DIM_VALUE (ID, UNIT_ID) values (value_id_seq.NEXTVAL, 1)';
  execute immediate 'insert into DIM_VALUE (ID, UNIT_ID) values (value_id_seq.NEXTVAL, 2)';

  execute immediate 'drop sequence value_id_seq';

end;

你也可以尝试不使用connect by level的顺序(你需要用你想要创建的记录数来改变someLimit

declare      
  current_max_value_id      number(8, 0);

begin

  select max(id) into current_max_value_id from DIM_VALUE;


  insert into DIM_VALUE (ID, UNIT_ID) 
   select level + nvl(current_max_value_id,0), level
     from dual
   connect by level <= someLimit;

end;