2016-08-29 29 views
0

我有了多条记录,但有些记录是从下表相似的表怎么算多行和PL返回结果/ SQL Oracle包

表1

Code   P1  P2   P3 
W   5  10   20 
S   5  10   20 
W   6  20   35 

如果我通过P1,P2,P3作为参数如下参数

select * 
from table 1 
where P1=5 and P2=10 and P3=20 

它将返回结果W和S,但只返回与paramteres相匹配的W. 我如何着手创建一个包含所有多个记录并返回相似代码的包?我对oracle不熟悉

+2

这是不太清楚,WH你试图达到......为什么只有W匹配?澄清你的问题,并给出所需输出的例子。 – Plirkee

+0

没有S匹配,我提到W和S应该返回,因为它有匹配的参数,但是如果你注意到了2个W,所以只有相应的W才会返回 – doe

+0

,所以你想返回一个与你提供的查询类似的结果,比如说这个:'从表1 中选择代码 ,其中P1 = 5,P2 = 10和P3 = 20' - 只有结果应该来自函数(包内)? – Plirkee

回答

1

这可以用pipelined函数来解决。

声明包和类型

CREATE OR REPLACE PACKAGE mytestpackage AS 

    TYPE my_record is RECORD(
      code  varchar2 
    ); 

    TYPE my_table IS TABLE OF my_record; 

function get_results(par1 number,par2 number,par3 number) RETURN my_table PIPELINED; 

end mytestpackage; 

封装体

CREATE OR REPLACE PACKAGE BODY as 

    function get_results(par1 number,par2 number,par3 number) RETURN my_table PIPELINED is 
     my_rec my_record:=null; 
     cursor myCursor(myp1 number,myp2 number,myp3 number) is 
     select code from table1 where P1=myp1 and P2=myp2 and P3=myp3 
     ; 

     begin 

     --loop through outputs 

     FOR item IN myCursor(par1,par2,par3) LOOP 
      my_rec:=null; 
      select item.code into my_rec from dual; 
      PIPE ROW(my_rec); 
     end loop; 
     return; 
     end; 
end mytestpackage; 

,最后用它

SELECT * FROM TABLE(mytestpackage.get_results(5,10,20));