0

i have a table that has multiple records, however some records are similar from the following table

Table 1

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

if i pass P1 , P2 , P3 as parameters following

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

it will return the result W and S,but only the W that matches to the paramteres. How do i go about creating a package that will count all the multiple records and return the codes that are similar? I am not familiar with oracle

4
  • 2
    it is not quite clear, what are you trying to achieve... why 'only W matches' ? clarify your question and give an example of desired output. Commented Aug 29, 2016 at 6:19
  • no S matches aswell, i mentioned that W and S should be returned because it has matching parameters, but if you notice theres 2 W's so only the corresponding W will get returned Commented Aug 29, 2016 at 6:23
  • so you want to return a similar result as the query you provided, say this one : select code from table1 where P1=5 and P2=10 and P3=20 - only a result should come from a function (inside a package) ? Commented Aug 29, 2016 at 6:29
  • yes, thats what im trying to do Commented Aug 29, 2016 at 6:33

1 Answer 1

1

This can be solved with pipelined functions.

declare package and types

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;

package body

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;

and finally use it

SELECT * FROM TABLE(mytestpackage.get_results(5,10,20));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.