我想执行一些存储在数据库中的 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/