【问题标题】:how to count multiple rows and return the result in pl/sql oracle packages如何计算多行并在 pl/sql oracle 包中返回结果
【发布时间】:2016-08-29 16:59:51
【问题描述】:

我有一个包含多条记录的表,但是有些记录与下表相似

表 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,但只返回与参数匹配的 W。 我该如何创建一个包来计算所有多条记录并返回相似的代码?我不熟悉oracle

【问题讨论】:

  • 不太清楚,你想达到什么目的......为什么'只有 W 匹配'?澄清您的问题并举例说明所需的输出。
  • 没有S匹配,我提到应该返回W和S,因为它有匹配的参数,但是如果你注意到有2个W,那么只有对应的W会被返回
  • 所以你想返回一个与你提供的查询类似的结果,比如这个:select code from table1 where P1=5 and P2=10 and P3=20 - 只有一个结果应该来自一个函数(在一个包内)?
  • 是的,这就是我想要做的

标签: oracle plsql package


【解决方案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));

【讨论】:

    猜你喜欢
    • 2021-04-28
    • 2021-12-04
    • 2021-06-08
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 2011-01-10
    • 2016-01-07
    • 1970-01-01
    相关资源
    最近更新 更多