使用 oracle pipelined 返回一个结果集;

Posted linbobo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用 oracle pipelined 返回一个结果集;相关的知识,希望对你有一定的参考价值。

1.使用

 1 create or replace package refcursor_pkg is
 2 
 3   -- Author  : mr.yang
 4   -- Created : 5/14/2017 5:13:42 PM
 5   -- Purpose : 
 6   type refcur_t is ref cursor return sys_product%rowtype;
 7   type refcur_t1 is ref cursor /*return sys_product%rowtype*/
 8   ;
 9   type refcur_t2 is ref cursor return base_member%rowtype;
10 
11   type outrec_typ is record(
12     v_pid   integer,
13     v_pcode varchar(4000),
14     v_pname varchar(4000));
15 
16   type outrecset is table of outrec_typ;
17   function f_trans(p refcur_t) return outrecset
18     pipelined;
19   function f_trans1(p refcur_t1) return outrecset
20     pipelined;
21   function f_trans1(sp refcur_t1, bm refcur_t2) return outrecset
22     pipelined;
23 
24 end refcursor_pkg;


2.body

 1 create or replace package body refcursor_pkg is
 2   function f_trans(p refcur_t) return outrecset
 3     pipelined as
 4     out_rec outrec_typ;
 5     in_rec  p%rowtype;
 6   
 7   begin
 8   
 9     loop
10       fetch p
11         into in_rec;
12       exit when p%notfound;
13       out_rec.v_pid   := in_rec.productid;
14       out_rec.v_pcode := in_rec.productcode;
15       out_rec.v_pname := in_rec.productname;
16     
17       pipe row(out_rec);
18     end loop;
19     close p;
20     return;
21   Exception
22     when others then
23       dbms_output.put_line(sqlcode || sqlerrm);
24   end f_trans;
25 
26   function f_trans1(p refcur_t1) return outrecset
27     pipelined as
28     out_rec outrec_typ;
29     in_rec  sys_product%rowtype;
30   begin
31     loop
32       fetch p
33         into in_rec;
34       exit when p%notfound;
35       out_rec.v_pid   := in_rec.productid;
36       out_rec.v_pcode := in_rec.productcode;
37       out_rec.v_pname := in_rec.productname;
38     
39       pipe row(out_rec);
40     end loop;
41     close p;
42     return;
43   Exception
44     when others then
45       dbms_output.put_line(sqlcode || sqlerrm);
46     
47   end f_trans1;
48   ---------------------------------------------------------
49   function f_trans1(sp refcur_t1, bm refcur_t2) return outrecset
50     pipelined as
51   
52     out_rec   outrec_typ;
53     in_rec    sys_product%rowtype;
54     in_rec_bm bm%rowtype;
55   begin
56   
57   
58     loop
59       fetch sp
60         into in_rec;
61       exit when sp%notfound;
62       /*   out_rec.v_pid   := in_rec.productid;
63          out_rec.v_pcode := in_rec.productcode;
64          out_rec.v_pname := in_rec.productname;
65          pipe row(out_rec);
66       out_rec.v_pid   := in_rec_bm.id;
67          out_rec.v_pcode := in_rec_bm.cnfullname;
68          out_rec.v_pname := in_rec_bm.pinyin;
69           pipe row(out_rec);*/
70     
71     end loop;
72     close sp;
73   
74     loop
75       fetch bm
76         into in_rec_bm;
77       exit when bm%notfound;
78       out_rec.v_pid   := in_rec_bm.id;
79       out_rec.v_pcode := in_rec_bm.cnfullname;
80       out_rec.v_pname := in_rec_bm.pinyin;
81       pipe row(out_rec);
82       
83       out_rec.v_pid   := in_rec.productid;
84       out_rec.v_pcode := in_rec.productcode;
85       out_rec.v_pname := in_rec.productname;
86       pipe row(out_rec);   
87     end loop;
88     close bm;
89   
90     return;
91   Exception
92     when others then
93       dbms_output.put_line(sqlcode || sqlerrm);
94     
95   end f_trans1;
96 
97 end refcursor_pkg;
View Code

3.结果:

 

 

以上是关于使用 oracle pipelined 返回一个结果集;的主要内容,如果未能解决你的问题,请参考以下文章

PL/SQL:, 如何将变量传递给 SELECT 语句并返回所有结果行

Oracle 流水线功能

基于 Oracle ADF LOV 的查询并返回结果集

包含使用 with 子句的 sql 的 oracle 管道函数

如何选择列表但从 Oracle 数据库返回行

Azure Synapse Pipeline Notebook 返回错误