【问题标题】:executing a function in sql plus在sql plus中执行一个函数
【发布时间】:2011-07-04 21:22:46
【问题描述】:

我在 oracle 中创建了一个函数,它在特定表中插入记录并根据函数中发生的情况返回输出。例如(ins_rec 返回编号)

如何调用这个函数并在sql plus中查看它的输出

【问题讨论】:

    标签: oracle function sqlplus


    【解决方案1】:

    正如另一个答案已经说过的,请致电 select myfunc(:y) from dual; ,但您可能会发现在 sqlplus 中声明和设置变量有点棘手:

    sql> var y number
    
    sql> begin
      2  select 7 into :y from dual;
      3  end;
      4  /
    
    PL/SQL procedure successfully completed.
    
    sql> print :y
    
             Y
    ----------
             7
    
    sql> select myfunc(:y) from dual;
    

    【讨论】:

      【解决方案2】:
      declare
        x number;
      begin
        x := myfunc(myargs);
      end;
      

      或者:

      select myfunc(myargs) from dual;
      

      【讨论】:

      • select myfunc(myargs) from dual; 为我工作。我遇到了delcare 版本的问题。
      • 我有一个调用过程的函数。如果我使用此方法调用该函数,则会收到 DML 写入异常。如果我使用“exec dbms_output....”调用,它可以正常工作
      • 您不能在select 语句中使用修改数据的函数。
      • @Alex78191 select 语句用于非 DML,即不对数据执行任何更新。你得到的错误是ORA-14551: cannot perform a DML operation inside a query
      【解决方案3】:

      一种选择是:

      SET SERVEROUTPUT ON
      
      EXEC DBMS_OUTPUT.PUT_LINE(your_fn_name(your_fn_arguments));
      

      【讨论】:

        猜你喜欢
        • 2012-02-27
        • 1970-01-01
        • 2016-12-26
        • 1970-01-01
        • 2012-05-11
        • 1970-01-01
        • 2019-11-09
        • 2012-09-22
        • 1970-01-01
        相关资源
        最近更新 更多