Print Thread
Page 1 of 2 1 2
Can a Package function return table?
#58071 06/20/18 07:55 AM
Joined: Jun 2018
Posts: 2
A
Member
OP Offline
Member
A
Joined: Jun 2018
Posts: 2
Can a Package function return table?

Hi all,

I got a set of data to display in a crystal report; But before the raw data be able to shown in crystal report, I have to massage the data via logic with insert and update statements to form my result table .
From MS SQL Server, there are stored procedure which I can write my own logic to interact with Tables or Values and if I end the stored procedure by a select statement, the SP can return the table result for me.

I knew that in Oracle, package act like stored procedure in MS SQL Server, but I dont know how to define the return type as a table in a package function....

But I only can get only 1 row, if i modified the SELECT statement that collect more than one row, the package can be compile but when I execute It will prompt error and said there are more than one row in result.

I also tried to use cursor and loop it, but still not working; May I ask can you guys give me some advise or help please?

Re: Can a Package function return table?
adamlevine #58072 06/20/18 10:52 AM
Joined: Jan 2006
Posts: 66
Lithuania
Member
Offline
Member
Joined: Jan 2006
Posts: 66
Lithuania
Hi adamlevine,
I think you need to be use a Pipelined Function:
https://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:19481671347143


Dalius
Re: Can a Package function return table?
Tichij #58073 06/21/18 03:28 AM
Joined: Mar 2011
Posts: 237
Russia
Member
Offline
Member
Joined: Mar 2011
Posts: 237
Russia
If you have to combine DML + Query logic in one call, then you need a function, returning CURSOR. Function can be within a package or standalone:

SQL Query
create or replace package body  MYPKG
..............
procedure P (result out sys_refcursor, Param1 date, Param2 number, ...)
is
begin
  -- Prepare output in RES_TABLE
  delete RES_TABLE ....
  insert into RES_TABLE ....
  update RES_TABLE ....
 open result for
  select *  from  RES_TABLE;
end;
.................
end MYPKG;

Then you could test your function as usual, the result set will open in a new tab of Test Window.


Best regards,
Maxim
Re: Can a Package function return table?
Maxmix #58083 06/25/18 10:56 AM
Joined: Mar 2004
Posts: 166
V
Member
Offline
Member
V
Joined: Mar 2004
Posts: 166
the use of pipelined keyword makes it easier.

create or replace package sorok is

type tr_sor is record(
n_ev integer,
d_dat date,
c_sor varchar2(400)
);
type ta_sor is table of tr_sor;

function sorokp(pn_ev integer) return ta_sor pipelined;

end sorok;
/

create or replace package body sorok is

function sorokp(pn_ev integer) return ta_sor pipelined is
r tr_sor;
begin
r:=null; r.n_ev:=pn_ev; r.d_dat:=to_date(pn_ev||'.01.01','yyyy.mm.dd'); r.c_sor:='year is '||r.n_ev;
pipe row(r);

r:=null; r.n_ev:=pn_ev; r.d_dat:=to_date(pn_ev||'.02.01','yyyy.mm.dd'); r.c_sor:='year is '||r.n_ev;
pipe row(r);

for i in 3..9 loop
r:=null; r.n_ev:=pn_ev; r.d_dat:=to_date(pn_ev||'.0'||i||'.01','yyyy.mm.dd'); r.c_sor:='year is '||r.n_ev;
pipe row(r);
end loop;

end;

end sorok;
/

the usage of it is :

select * from table (sorok.sorokp(2018))

the result

2

Re: Can a Package function return table?
adamlevine #58084 06/25/18 09:47 PM
Joined: Sep 2003
Posts: 387
London, UK
Member
Offline
Member
Joined: Sep 2003
Posts: 387
London, UK
[quote]I knew that in Oracle, package act like stored procedure in MS SQL Server
[/quote]
No, a procedure in Oracle is like a procedure in SQL Server. A PL/SQL package is a container for multiple procedures and functions etc, like a C library or a Perl module. As far as I know, SQL Server has no equivalent.

Re: Can a Package function return table?
William Robertson #58085 06/26/18 07:16 AM
Joined: Mar 2011
Posts: 237
Russia
Member
Offline
Member
Joined: Mar 2011
Posts: 237
Russia
[quote]the use of pipelined keyword makes it easier[/quote]
But any DML prohibited within pipelined function.
Serious limitation, I'd say


Best regards,
Maxim
Re: Can a Package function return table?
Maxmix #58086 06/26/18 07:40 AM
Joined: Jun 2017
Posts: 43
Dresden, Germany
J
Member
Offline
Member
J
Joined: Jun 2017
Posts: 43
Dresden, Germany
Hi, DML is allowed if You use pragma autonomous_transaction.
Regards

Re: Can a Package function return table?
Jens Naumann #58100 06/28/18 05:26 AM
Joined: Mar 2011
Posts: 237
Russia
Member
Offline
Member
Joined: Mar 2011
Posts: 237
Russia
[quote=Jens Naumann]Hi, DML is allowed if You use pragma autonomous_transaction.
Regards[/quote]

Nice!
I wish I learned it before...


Best regards,
Maxim
Re: Can a Package function return table?
Jens Naumann #58101 06/28/18 05:26 AM
Joined: Mar 2011
Posts: 237
Russia
Member
Offline
Member
Joined: Mar 2011
Posts: 237
Russia

Deleted duplicate

Last edited by Maxmix; 06/28/18 05:28 AM.

Best regards,
Maxim
Re: Can a Package function return table?
Jens Naumann #58102 06/28/18 05:26 AM
Joined: Mar 2011
Posts: 237
Russia
Member
Offline
Member
Joined: Mar 2011
Posts: 237
Russia
Deleted duplicate

Last edited by Maxmix; 06/28/18 05:28 AM.

Best regards,
Maxim
Page 1 of 2 1 2

Moderated by  support 

Link Copied to Clipboard
Powered by UBB.threads™ PHP Forum Software 7.7.4
(Release build 20200307)
Responsive Width:

PHP: 7.1.33 Page Time: 0.095s Queries: 15 (0.032s) Memory: 2.5650 MB (Peak: 3.0430 MB) Data Comp: Off Server Time: 2024-05-18 20:44:58 UTC
Valid HTML 5 and Valid CSS