Single quote in dbms_output statement?

馋奶兔 提交于 2019-12-10 19:06:04

问题


I need to include single quotes in dbms_output statement. I've tried this:

 dbms_output.put_line('\''first_name||'\'');

Here first_name is variable; I need to display this inside single quotes.


回答1:


you'd escape by doubling up:

 dbms_output.put_line('''' || first_name || '''');

or using the q-quote mechanism:

 dbms_output.put_line(q'[']'||first_name||q'[']');

eg:

SQL> var b1 varchar2(30)
SQL> exec :b1 := 'this is a test';

PL/SQL procedure successfully completed.

SQL> exec  dbms_output.put_line(q'[']'||:b1||q'[']');
'this is a test'

PL/SQL procedure successfully completed.

SQL> exec  dbms_output.put_line(''''||:b1||'''');
'this is a test'

PL/SQL procedure successfully completed.

SQL> exec  dbms_output.put_line(chr(39)||:b1||chr(39));
'this is a test'

PL/SQL procedure successfully completed.



回答2:


Note that this is only for PL/SQL and must be active.

begin
DBMS_OUTPUT.PUT_LINE('DELETE FROM MYTABLE WHERE MYFIELD <> ''A'';');
end;
/

Assuming you want to match a CHAR 'A'.



来源:https://stackoverflow.com/questions/14690022/single-quote-in-dbms-output-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!