Hi Srivathsava,
Hopefully, the following will help.
-- Create a test table first and populate it with some data.
create table xx_emp_apprvr_tbl(
Person_id number(6),
Transaction_id number(8)
);
insert into xx_emp_apprvr_tbl values (1,1);
insert into xx_emp_apprvr_tbl values (2,1);
insert into xx_emp_apprvr_tbl values (3,1);
insert into xx_emp_apprvr_tbl values (616,1);
insert into xx_emp_apprvr_tbl values (1,2);
insert into xx_emp_apprvr_tbl values (777,2);
insert into xx_emp_apprvr_tbl values (666,300);
insert into xx_emp_apprvr_tbl values (1002, 450);
commit;
CREATE OR REPLACE PACKAGE abc_pkg AS
-- Create a record type for the data we want to return. Doing it as
-- a record in this manner allows the column_names to be explicitly
-- specified. In this case, we will see PERSON_ID rather than the
-- default COLUMN_NAME.
TYPE Person_id_rec is record (person_id xx_emp_apprvr_tbl.Person_id%type);
-- Now we must create a type of a table of the above records, as this
-- is what the pipelined functions return, tables.
TYPE Person_id_table IS TABLE OF Person_id_rec;
-- Finally, our pipelined function to return all the person_id_recs for
-- a given transaction_id passed in.
FUNCTION abc_fun(TID NUMBER) RETURN Person_id_table PIPELINED;
END abc_pkg;
/
CREATE OR REPLACE PACKAGE BODY abc_pkg AS
FUNCTION abc_fun(TID NUMBER) RETURN Person_id_table PIPELINED IS
BEGIN
-- Loop around each person_id in the table, who has a given
-- transaction_id. For each record found, pipe it back to
-- the caller.
FOR rec IN (select person_id
from xx_emp_apprvr_tbl
where transaction_id = TID)
LOOP
PIPE ROW(rec);
END LOOP;
-- This is a function, so we must return something, but as this
-- is a pipelined function, RETURN on its own will do.
RETURN;
END abc_fun;
END abc_pkg;
/
select * from table(abc_pkg.abc_fun(1)) order by 1;
PERSON_ID
1
2
3
616
Ok, an explanation.
You must return a table of some kind from a pipelined function. As you wish to return a single column, the person_id, we first create a TYPE of person_id_rec in the package itself. Doing this as a record is useful because it allows you to name the column(s) that your function will return. Here I've simply named it PERSON_ID. You will note that I have "anchored" the PERSON_ID column in the record type, to the table's PERSON_ID column by the way I've used table_name.column_name%type in the creation of the type. That way, if PERSON_ID in the table is ever changed, you won't have to search all your code to make changes, Oracle will simply recompile it on the fly for you! You should almost never hard code a data type when it relates to something in a table or view.
Next we have to create a table of the record type. This is what the pipelined function will return. In this case, I call it person_id_table and it is simply a table of the above record type.
Finally, we have your abc_fun function, which takes in a single parameter of the correct type for a transaction_id in the table (you see that I have anchored that as well) and returns a value which is a table type, the one we have just created, person_id_table.
In the package body, the function is pretty simple, it enters a loop for each row in the table which has the supplied transaction_id. Each row is referred to as "rec" and each row is simply PIPEd out to the caller. Functions must return something, but pipelined functions just need to RETURN.
When you execute the SQL statement:
select * from table(abc_pkg.abc_fun(1));
You will see that the column name in the output (as shown above) is named PERSON_ID as we created in our record.
HTH
Cheers,
Norm. [TeamT]