【问题标题】:PL/SQL how to get result from a function that returns type table ofPL / SQL如何从返回类型表的函数中获取结果
【发布时间】:2019-04-16 23:45:35
【问题描述】:

我需要调用一个返回TABLE OF MyTable%ROWTYPE的函数。

我做了以下事情:

 DECLARE
   TYPE type_tab1 IS TABLE OF  Table1%ROWTYPE; 
   v_result1 type_tab1;
BEGIN

   v_result1 := myfunction(p_1, p_2, p_3);

END;

但它不起作用 - 我收到一个错误:

PLS-00382:表达式类型错误。

我做错了什么?

【问题讨论】:

  • 请发布您遇到的错误。
  • 您提供的信息不足以为您提供解决方案。向我们展示Table1 的定义、myfunction 的内容以及它的返回类型(对象及其嵌套表)的定义
  • v_result1 需要定义为 myfunction 返回的任何类型,而不是您的块中定义的某些本地类型。

标签: oracle plsql


【解决方案1】:

你没有提供一些可能有用的信息,所以 - 看看这个例子,看看它是否有帮助。

首先,准备好我们需要的一切:

SQL> create or replace type td as object
  2    (deptno number,
  3     dname  varchar2(20),
  4     loc    varchar2(20));
  5  /

Type created.

SQL> create or replace type tty is table of td;
  2  /

Type created.

功能:

SQL> create or replace function myfunction(p_deptno in number)
  2  return tty is
  3    l_tty tty := tty();
  4  begin
  5    select td(deptno, dname, loc)
  6      bulk collect into l_tty
  7      from dept
  8      where deptno = p_deptno;
  9
 10    return l_tty;
 11  end;
 12  /

Function created.

测试:

SQL> select * from table(myfunction(10));

    DEPTNO DNAME                LOC
---------- -------------------- --------------------
        10 ACCOUNTING           NEW YORK

你的代码:

SQL> set serveroutput on
SQL> declare
  2    v_result tty;
  3  begin
  4    v_result := myfunction(10);
  5    dbms_output.put_line(v_result(1).dname);
  6  end;
  7  /
ACCOUNTING

PL/SQL procedure successfully completed.

SQL>

【讨论】:

  • 我尝试使用 select * from table(mypackage.myfunction(id =>10, field1=>'Test', status => 'Active')); 查看 TOAD 中的数据,但收到错误消息,指出 mypackage 是无效的数据类型。如何在 TOAD 中运行它?
猜你喜欢
  • 2013-11-07
  • 2010-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多