使用dbms_sql.parse执行包含在clob中的sql语句

时间:2014-07-25 14:05:51

标签: database oracle oracle11g

我想执行存储在数据库中clob中的一些sql语句。 我想使用dbms_sql.parse和clob作为输入参数。

我在11.2 Oracle数据库上尝试作为测试用例的代码:

为插页制作表格:

create table table1 (t1 number(8), t2 varchar2(1), t3 varchar2(1));

失败的陈述:

DECLARE
  cursor makeclob is
   select 'insert into table1 (t1,t2,t3) values ('||rownum||', ''X'',''I'');' stat
   from dual
   connect by level < 10000;

  testcl clob;
  opencu integer;
  err integer;
BEGIN
  for rec in makeclob loop
    testcl := testcl || rec.stat || '\n';
  end loop;
  testcl := testcl || 'commit;'|| '\n';
  opencu := dbms_sql.open_cursor;

  dbms_sql.parse(opencu,testcl,dbms_sql.native);

  err := dbms_sql.execute(opencu);
  dbms_sql.close_cursor(opencu);
END;

此语句因以下错误而失败:

ORA-00911: invalid character.
ORA-06512: at "SYS.DBMS_SQL", line 1250
ORA-06512: at line 17
00911. 00000 -  "invalid character"
*Cause:    identifiers may not start with any ASCII character other than
           letters and numbers.  $#_ are also allowed after the first
           character.  Identifiers enclosed by doublequotes may contain
           any character other than a doublequote.  Alternative quotes
           (q'#...#') cannot use spaces, tabs, or carriage returns as
           delimiters.  For all other contexts, consult the SQL Language
           Reference Manual.
*Action:

有谁知道我的陈述有什么问题?

1 个答案:

答案 0 :(得分:1)

您应该将解析后的语句包装在

之间
  

BEGIN

  

END

也可以使用chr(13)代替&#39; \ n&#39;。

我稍微调整了你的代码,所以看看这个:

DECLARE
  cursor makeclob is
   select 'insert into table1 (t1,t2,t3) values ('||rownum||', ''X'',''I'');' stat
   from dual
   connect by level < 10000;

  testcl clob;
  opencu integer;
  err integer;
BEGIN
  testcl := 'BEGIN'||chr(13);
  for rec in makeclob loop
    testcl := testcl || rec.stat ||chr(13);
  end loop;
  testcl := testcl || 'commit;'||chr(13);
  testcl := testcl || 'END;';
  opencu := dbms_sql.open_cursor;

  dbms_sql.parse(opencu,testcl,dbms_sql.native);

  err := dbms_sql.execute(opencu);
  dbms_sql.close_cursor(opencu);
END;