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

如何在PLSQL Developer中测试包含DML的Oracle函数?

  •  2
  • James  · 技术社区  · 16 年前

    select function_name() from dual;
    

    如果函数包含DML(在本例中,有些插入操作用于记录传递给函数的参数),则不允许执行上述查询。(ORA-14551)

    如何选择/查看此函数的返回值?

    如果我在plsql developer中选择“test”,plsqldev将生成如下内容:

    declare
      -- Non-scalar parameters require additional processing 
      result xmltype;
    begin
      -- Call the function
      result := find_person(as_surname => :as_surname,
                           as_given => :as_given,
                           ad_birth_date_from => :ad_birth_date_from,
                           ad_birth_date_to => :ad_birth_date_to,
                           as_gender => :as_gender);
    end;
    

    select result from dual;
    

    在开始/结束块内部产生

    ORA-06550: PLS-00428: an INTO clause is expected in this SELECT statement
    
    6 回复  |  直到 16 年前
        1
  •  2
  •   Robert Giesecke    16 年前

    将“result”更改为“:result”,然后单击变量网格左上角的小箭头。 它应该添加“result”作为绑定变量,您可以指定其类型。

    你的剧本可能是这样的:

    declare
      result xmltype;
    begin
      result := find_person(as_surname => :as_surname,
                            as_given => :as_given,
                            ad_birth_date_from => :ad_birth_date_from,
                            ad_birth_date_to => :ad_birth_date_to,
                            as_gender => :as_gender);
      if result is null then
        :result := null;
      else
        :result := result.GetClobVal();
      end if;
    end;
    

    如您所见,这基本上是PL/SQL Dev为您创建的,只是处理了如何以PL/sqldev理解的方式返回xmltype。

    如果要返回结果集,可以返回游标:

    begin
      ...
      open :someCursor for 
        select 1 from ...;
      ...
    

        2
  •  1
  •   jva    16 年前

    我没有使用xmltype,但是文档提供了以下选项:

    dbms_output.put_line(result.getStringVal());
    
        3
  •  0
  •   James    16 年前

    添加

    pragma autonomous_transaction 
    

    declare 块允许从双

    select find_person(arguments) from dual;
    

    由于函数中的DML只用于记录传递给函数的参数,因此它是自治事务的一种可接受的用法,但在其他情况下应避免

        4
  •  0
  •   Venkataramesh Kommoju    16 年前

    pragma_autonomous_事务是一种方法。

    但是为了测试原始数据库,很少有开源工具来测试SQL/plsql,比如DBUNIT、utPLSQL等。

    这些是SQL和plsql的单元测试工具

        5
  •  0
  •   Rene    16 年前

    PLSQL developer中的测试屏幕有两个部分。在上面的部分,你会找到你在问题中显示的代码。测试函数生成的代码已用bind variables::as_lash,:as_given等替换了函数的变量。 在屏幕的下部,您可以输入这些参数的值并查看结果的值。

        6
  •  0
  •   David Aldridge    16 年前

    DBMS_Output.Put_Line(result); 在你的密码里。