Database 使用dbms_sql.parse执行clob中包装的sql语句

Database 使用dbms_sql.parse执行clob中包装的sql语句,database,oracle,oracle11g,Database,Oracle,Oracle11g,我想执行一些存储在数据库clob中的sql语句。 我想使用带有clob作为输入参数的dbms_sql.parse 我在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

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

我在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;

有人知道我的语句有什么问题吗?

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

开始

结束

也使用chr(13)而不是“\n”

我对您的代码做了一点修改,请看以下内容:

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;
声明
游标makeclob是
选择“插入到表1(t1、t2、t3)值中”(“| | rownum”|“、“X”、“I”);”斯达
来自双重
按<10000级连接;
testcl-clob;
opencu整数;
误差整数;
开始
testcl:=“开始”| chr(13);
对于makeclob循环中的rec
testcl:=testcl | | rec.stat | | chr(13);
端环;
testcl:=testcl | |提交;'||chr(13);
testcl:=testcl | |“END;”;
opencu:=dbms\u sql.open\u游标;
parse(opencu、testcl、dbms_sql.native);
err:=dbms_sql.execute(opencu);
dbms_sql.close_游标(opencu);
结束;
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;