在变量中存储包含单引号和引号等字符的字符串

Storing string containing characters like single, ampersand qoutes in a variable

我的程序将接收字符串(电子邮件正文消息)作为输入参数,并将其分配给一个变量。它可以包含单引号和符号等字符。我该如何处理这种情况并将字符串原样分配给 clob 变量。

SET SERVEROUTPUT ON

DECLARE

email_body CLOB;

BEGIN

email_body:=to_clob('Hel's Message');

DBMS_OUTPUT.PUT_LINE(length(email_body));

EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUt_LINE(SQLCODE||' '||SQLERRM);

END;

Error:

ORA-06550: line 7, column 22: PLS-00103: Encountered the symbol "S" when expecting one of the following:

您可以尝试以下方法:

SET SERVEROUTPUT ON
/* disable variable substitution */
SET DEFINE OFF 

DECLARE
email_body CLOB;
BEGIN
email_body:=to_clob(q'[Hel's M&ssage]'); /* Q operator to avoid issues with quotes */

    DBMS_OUTPUT.PUT_LINE(length(email_body));

EXCEPTION
    WHEN OTHERS THEN
    DBMS_OUTPUT.PUt_LINE(SQLCODE||' '||SQLERRM);
END;
/

这里我用 to prevent variable substitution and the Oracle Q quote operator来处理带引号的字符串