【问题标题】:How to handle pipe and bind variable in execute immediate如何在立即执行中处理管道和绑定变量
【发布时间】:2018-03-24 08:21:22
【问题描述】:

我有这样的事情:

declare

begin

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

end;

我想动态运行开始块,但不能通过立即执行来实现。

以下代码提示输入 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 中。

请帮忙

【问题讨论】:

    标签: sql database oracle plsql


    【解决方案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 中使用绑定变量 - 但我假设您已经掌握了原理。

    【讨论】:

    • 嗨 Wernfried,开始 dbms_output.put_line('some text'|| :bind_variable|| 'again some text');结尾;如果这有效,那么执行立即中的 bind_variable 也应该有效
    • 你的真实密码是什么?肯定dbms_output.put_line('some text'|| :bind_variable|| 'again some text'); 不会工作。
    • 您尝试运行代码了吗?它在匿名块中为我工作,只是我想立即执行。
    • 我不明白。再次,请向我们展示您的真实代码,然后我们可以告诉您出了什么问题。
    【解决方案2】:

    您不需要EXECUTE IMMEDIATE。使用VARIABLE 命令。当作为脚本执行时,它可以在 SQL*Plus 和 SQL developer 和 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.
    

    如果您想提示用户输入,请使用 &amp; 替换变量。

    dbms_output.put_line('some text &sub_variable again some text');
    

    【讨论】:

    猜你喜欢
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多