代码之家  ›  专栏  ›  技术社区  ›  gavenkoa

Oracle:从“select from dual”调用函数

  •  2
  • gavenkoa  · 技术社区  · 7 年前

    select AGENTS_REGISTRATION() from dual;
    

    declare
      x NUMBER;
    begin
      select AGENTS_REGISTRATION() into x from dual;
    end;
    /
    

    什么都不做。另一种形式进行了必要的更改:

    declare
      x NUMBER;
    begin
      x := AGENTS_REGISTRATION();
    end;
    /
    

    2 回复  |  直到 7 年前
        1
  •  4
  •   Goran Stefanović    7 年前

    Oracle不允许 DML 查询内的操作。调用函数 inserts/updates/deletes

    ORA-14551:无法在查询内执行DML操作

    如果出现以下情况,查询将不会引发错误: exception 被困在函数内-使用它非常危险 WHEN OTHERS ORA-14551 直接使用 pragma exception_init )正如您在本例中看到的那样,无需重新引发错误。

    DML 如果函数定义为,则查询中的操作 autonomous transaction pragma autonomous_transaction )-但这将导致更多问题。

    解决方案是不在查询内调用函数-这样做肯定没有意义。

        2
  •  1
  •   Toolkit    5 年前

    检查 http://www.dba-oracle.com/t_ora_14551_cannot_perform_a_dml_operation_inside_a_query.htm

    create or replace function myFunction return varchar2 as
    begin
    update emp set empno = empno +1 where empno = 0;
    return 'Yeah';
    end; 
    /
    
    
    SQL> var myVar VARCHAR2
    SQL> call myFunction() INTO :myVar;
    SQL> print myVar