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

标签 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 ('||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:

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

最佳答案

你应该把你解析过的语句放在

之间

BEGIN

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;

关于database - 使用 dbms_sql.parse 执行包含在 clob 中的 sql 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24957633/

相关文章:

database - 如何在postgresql 8.1下转储特殊表的部分字段

sql - 在单元格上执行 Oracle 查询的最佳方式?

database - 会简化插入到具有自动生成的主键的表吗?

sql - 在 SQL 查询中使用 APEX 应用程序项

带有 IN 子句参数的 Oracle 存储过程

linux - Ubuntu 上的 Oracle 11 XE 给出 ORA-27101 : shared memory realm does not exist

php - oci_bind_by_name : PHP Fatal error: Only variables can be passed by reference

oracle11g - 如何使 CLOBAGG 函数对结果进行排序?

mysql - 在考虑高性能的情况下,为相似类型的数据保留一个表或多个表,哪个是最好的

oracle - 如何将大表从 oracle db 到 hdfs?