在执行立即语句中使用 like 'string token' 时遇到单引号问题

having issues with single quotes using like 'string token' in execute immediate statement

这是代码部分,我正在使用 ' ' blah ' ' 来转义单引号,但我猜它不起作用:

declare
my_func varchar2(20) :='test_func';

begin 
execute immediate 'insert into TABLE_TEST (OUTPUT) select ' || my_func || ' from dual where TABLE_TEST.FUNCTION_NAME like ' 'VALIDATION1_%' '  ';
end;

我收到以下错误:

PLS-00103: Encountered the symbol "VALIDATION1_%" when expecting one of the following:

   & = - + ; < / > at in is mod remainder not rem return
   returning <an exponent (**)> <> or != or ~= >= <= <> and or
   like like2 like4 likec between into using || bulk member
   submultiset
The symbol "* was inserted before "VALIDATION1_%" to continue.

看起来你正试图用另一个单引号 escape the single quote(这很好),但两者之间有一个额外的 space。那必须走了。

改变

' from dual where TABLE_TEST.FUNCTION_NAME like ' 'VALIDATION1_%' '  '

' from dual where TABLE_TEST.FUNCTION_NAME like ''VALIDATION1_%''  '

在这种情况下,对文字字符串使用新的 q 语法要简单得多,例如:

execute immediate 'insert into TABLE_TEST (OUTPUT) select ' || my_func ||
  q[' from dual where TABLE_TEST.FUNCTION_NAME like 'VALIDATION1_%' ]';