Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Wednesday 26 September 2018

Oracle Function returning multiple values

We Can Return multiple values from the function in PLSQL by following steps given below :



  • Create an OBJECT TYPE that contain the columns required as output
  • Create a Collection of OBJECT TYPE you can use any but i prefer nested table.
  • Create a FUNCTION with return type of collection variable.



create or replace type rec_emp as OBJECT
(
  emp_name varchar2(100),
  emp_sal  number
);
/
Create or replace type tab_emp is table of rec_emp;
/

create or replace function multi_ret return tab_emp is
  temp_tab_emp tab_emp := TAB_EMP();
 -- Emp_NAME VARCHAR2(100);
 -- EMP_SAL NUMBER;
begin
      TEMP_TAB_EMP.EXTEND();
      select REC_EMP(E.ENAME,E.SAL)
      BULK COLLECT INTO TEMP_TAB_EMP 
      from emp E;
    return temp_tab_emp;
end;

   
 After function is created use the table function to call the function.

SELECT * FROM TABLE(MULTI_RET);

No comments:

Post a Comment