如何在执行立即执行管道和绑定变量

时间:2018-03-24 08:21:23

标签: sql database oracle plsql

我有这样的事情:

declare

begin

dbms_output.put_line('some text'|| :bind_variable||'again some text');

end;

我想动态运行begin块但不能通过执行立即执行。

以下代码提示bind_variable:

declare
begin
execute immediate '
dbms_output.put_line(''some text''|| ':bind_variable'||''again some text'');
'
;
end;

但抛出以下错误:

Error starting at line : 3 in command -
declare

begin

execute immediate '
dbms_output.put_line(''some text''|| ':bind_variable'||''again some text'');
'
;
end;

Error report -
ORA-06550: line 5, column 39:
PLS-00103: Encountered the symbol "" 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 || multiset bulk
   member submultiset
The symbol "* was inserted before "" to continue.
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

我不确定如何将开始块放在execute immediate

请帮忙

2 个答案:

答案 0 :(得分:1)

您不需要绑定变量,您可以运行

bind_variable := ' some variable text';
dbms_output.put_line('some text'|| bind_variable||'again some text');

如果您真的需要动态声明,可以使用

execute immediate 
   'dbms_output.put_line(''some text :bind_variable again some text'')'  
   USING bind_variable;

但是,我不确定你是否可以在dbms_output.put_line中使用绑定变量 - 但我认为你有原则。

答案 1 :(得分:0)

您不需要EXECUTE IMMEDIATE。使用VARIABLE命令。它可以在SQL * Plus以及SQL开发人员和Toad中作为脚本执行。

VARIABLE bind_variable VARCHAR2(20)

EXEC :bind_variable := ' TEXT IN BETWEEN '

set serveroutput on
declare
begin

 dbms_output.put_line('some text'|| :bind_variable||'again some text');

end;
/

some text TEXT IN BETWEEN again some text


PL/SQL procedure successfully completed.

如果要提示用户输入,请使用&

使用替换变量
dbms_output.put_line('some text &sub_variable again some text');
相关问题