oracle procedure returning table of custom data type record

旧时模样 提交于 2019-12-11 19:48:58

问题


I created a custom data type of the type "object"

create type myrecord is object(col1 varchar2(10),col2 varchar2(10));

I created a table which is of the type record

create type mytableobject is table of myrecord;

Now i have filled the table "mytableobject" with data using

"execute immediate" (select * from table1) bulk collect into mytableobject.

I want to return this mytableobject in a STORED procedure.

How do i achieve this? And how do i call the procedure?


回答1:


You can use your own type as an OUT parameter to a stored procedure. The table population you showed doesn't make much sense, so I think this is what you must really be doing:

create or replace procedure myproc(mytable out mytableobject) is
begin
  select myrecord(col1, col2)
  bulk collect into mytable
  from table1;
end myproc;
/

You can then call that from another procedure or anonymous block by declaring a local variable of that type and passing it in:

declare
  tab mytableobject;
begin
  myproc(tab);
end;
/

It seems more likely that you want a function, particularly since you mentioned returning it. That's pretty much the same:

create or replace function myfunc
return mytableobject is
  mytable mytableobject;
begin
  select myrecord(col1, col2)
  bulk collect into mytable
  from table1;

  return mytable;
end myfunc;
/

declare
  tab mytableobject;
begin
  tab := myfunc;
end;
/

But more usefully you can call the function from SQL:

select * from table(myfunc);

SQL Fiddle.



来源:https://stackoverflow.com/questions/23433506/oracle-procedure-returning-table-of-custom-data-type-record

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!