PL/SQL how to get result from a function that returns type table of

时间:2019-04-16 23:45:35

标签: oracle plsql

I need to call a function that returns TABLE OF MyTable%ROWTYPE.

I did the following:

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

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

END;

But it does not work - I am getting an error:

PLS-00382: expression is of wrong type.

What am I doing wrong?

1 个答案:

答案 0 :(得分: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>