Tuesday, November 24, 2015

Oracle Function Returns Table Pl-Sql

Create a table type


CREATE OR REPLACE TYPE XX_REC_TYPE AS     OBJECT
    (
        a1 VARCHAR2(50) ,
        a2      VARCHAR2 (50)
        );


CREATE OR REPLACE TYPE XX_TBL_TYPE
IS
    TABLE OF XX_REC_TYPE;
    


create a function with return table type


 CREATE OR REPLACE FUNCTION XX_SVC(
        p_input VARCHAR2 )
    RETURN XX_TBL_TYPE
AS
    ord_tbl XX_TBL_TYPE:=XX_TBL_TYPE ();
BEGIN

    FOR r1 IN
    (
        SELECT  value1,value2 from xx_tbl
                AND value1 = p_input
    )
    LOOP
        ord_tbl.Extend;
        ord_tbl(ord_tbl.last) := XX_REC_TYPE (r1.value1,r1.value2);
    END LOOP;

RETURN( ord_tbl );

END;   


To call the function use the statement below


select *  from TABLE( XX_SVC('your_input') );




No comments:

Post a Comment