PL / SQL如何从返回类型表的函数中获取结果

问题描述 投票:0回答:1

我需要调用一个返回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:表达式类型错误。

我究竟做错了什么?

oracle plsql
1个回答
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>
© www.soinside.com 2019 - 2024. All rights reserved.