2

只需选择函数即可选择不包含 DML 的 Oracle 存储函数的返回值:

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
4

6 回答 6

2

change "result" to ":result" and click on the little arrow thingy in the top left corner of the variables grid. It should add "result" as a bind varibale and you can specify its type.

In your case the best options are clob or PL/SQL string.

And your script could look like so:

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;

As you can see, it is basically what PL/SQL Dev has created for you, except for the handling of how to return the xmltype in a way that PL/SQL Dev understands.

if you want to return a resultset, you can return cursors:

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

You have to change the type of "someCursor" in the variables grid to "cursor" or it is not going to work.

于 2009-12-14T10:59:54.887 回答
1

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

dbms_output.put_line(result.getStringVal());
于 2009-12-14T10:33:00.453 回答
0

SQL Developer 与 DBMS_OUTPUT 配合得很好。它在脚本输出、结果等旁边有一个单独的选项卡。只需单击“启用输出”小按钮并DBMS_Output.Put_Line(result);在您的代码中使用。

于 2009-12-14T11:05:16.653 回答
0

adding

pragma autonomous_transaction 

to the function in the declare block allows it to be selected from dual

select find_person(arguments) from dual;

Since the DML in the function is simply for logging the arguments passed in to the function, it is an acceptable use of autonomous_transaction, but otherwise should be avoided

于 2009-12-13T01:54:34.100 回答
0

PLSQL developer 中的测试屏幕有两个部分。在上半部分,您将找到您在问题中显示的代码。测试函数生成的代码已将函数的变量替换为绑定变量::as_surname、:as_given 等。在屏幕下方,您可以输入这些参数的值并查看结果值。

于 2009-12-14T07:43:05.040 回答
0

pragma_autonomous_transaction 是一种方式。

但是为了在不影响您的原始数据库的情况下进行测试,很少有开源工具可以测试您的 SQL / PLSQL,例如 DBUNIT、utPLSQL 等。

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

于 2009-12-13T09:21:58.683 回答