引用的字符串未在plsql块中正确终止

问题描述 投票:1回答:2

我的表ot.config为:enter image description here

我的表ot.stu为:

enter image description here

我的plsql块为:

declare
vquery long;
cursor c1 is
select * from ot.config1;
begin
for i in c1
loop
vquery :='INSERT INTO all_acc_num(code,nbr_value) 
select '''||i.acct_nbr||''','||i.acct_nbr||' from ot.stu';
commit;
execute immediate vquery;
end loop;

end;
/

但是,当我编译时,我得到了如下错误:

ORA-01756: quoted string not properly terminated
ORA-06512: at line 10

如何处理此错误?

我的预期输出是:enter image description here

oracle plsql oracle12c
2个回答
1
投票

您必须处理acct_nbr中的撇号,请使用replace()。另外,您还需要为a添加别名ot.stu,因为您在正则表达式中使用了a.name

declare
  vquery varchar2(32767);
  cursor c1 is select * from config1;
begin
  for i in c1 loop
    vquery :='INSERT INTO all_acc_num(code,nbr_value) 
              select '''||replace(i.acct_nbr, '''', '''''')||''', '||i.acct_nbr||' from ot.stu a';
    dbms_output.put_line(vquery);
    -- execute immediate vquery;
  end loop;
end;

[dbfiddle] >>

使用dbms_output查看您的代码是否生成有效的sql。确定后,请注释dbms_output,然后取消注释execute immediate


0
投票

[您可以在将绑定值用作文字值时使用绑定参数来传递值,而在想要使用它动态生成列名时可以使用字符串连接。

© www.soinside.com 2019 - 2024. All rights reserved.