GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Oracle  >  PL/SQL
Go To First  |  Previous Question  |  Next Question 
 PL/SQL  |  Question 213 of 241    Print  
Function return more than one value
Hi,

My questions is " Can function return more than one value".
I have gone through the all answers relevant to my question was been posted earlier. But all answers are confusing me.
Few people said directly that function can't return more than one value and few are telling that function can return more that one value using OUT parameter or ref cursor.
My understanding is if out can't use as a parameter in function how we can return more than one vale.

Can anyone clear me with exact program example.
it would really favour for me if any any reply soon as I am in urged need of answer.

Please reply ASAP.
Regards,
ORA



  
Total Answers and Comments: 8 Last Update: July 23, 2009     Asked by: satyam_Ora 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
May 22, 2008 06:31:56   #1  
ramesh_348401 Member Since: May 2008   Contribution: 1    

RE: Function return more than one value

you define that function must return a char value because. it max limit. chop all variable in that variable with remember the position of each value width. return that value and use substr function as per ur reqd.

ramesh


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
May 28, 2008 23:49:16   #2  
krishnaindia2007 Member Since: September 2007   Contribution: 854    

RE: Function return more than one value

As per oracle documentation
The main purpose of a function is to take zero or more arguments and return sigle value.
Avoid using OUT and INOUT parameter modes with functions.
It is a poor programming practice to return more than one value using functions.

But we can return more than one value using out parameter as follows

DECLARE
A NUMBER : 10;
B NUMBER ;
C NUMBER ;
D NUMBER ;
FUNCTION TEST_FUN (A IN NUMBER B OUT NUMBER C IN OUT NUMBER) RETURN NUMBER IS
D NUMBER;
BEGIN
B : A;
C : B + A;
D : A + B + C;
RETURN (D);
END ;
BEGIN
D : TEST_FUN(A B C);
DBMS_OUTPUT.PUT_LINE(' A ' || A);
DBMS_OUTPUT.PUT_LINE(' B ' || B);
DBMS_OUTPUT.PUT_LINE(' C ' || C);
DBMS_OUTPUT.PUT_LINE(' D ' || D);
END;


Using REF CURSOR we can return objects as return type. Here is example
DECLARE
TYPE EMP_REFCUR IS REF CURSOR;
EMP_CURVAR EMP_REFCUR;
TYPE EMP_REC IS RECORD
(
V_EMPNO EMP.EMPNO TYPE
V_ENAME EMP.ENAME TYPE
V_JOB EMP.JOB TYPE
V_SAL EMP.SAL TYPE
);
EMP_RECVAR EMP_REC;
FUNCTION TEST_FUN (V_EMPNO IN EMP.EMPNO TYPE) RETURN EMP_REFCUR IS
BEGIN
OPEN EMP_CURVAR FOR SELECT EMPNO ENAME JOB SAL FROM EMP WHERE EMPNO V_EMPNO;
RETURN(EMP_CURVAR);
END ;
BEGIN
EMP_CURVAR : TEST_FUN(7369);
FETCH EMP_CURVAR INTO EMP_RECVAR;
DBMS_OUTPUT.PUT_LINE('EMPLOYEE NAME IS ' || EMP_RECVAR.V_ENAME);
DBMS_OUTPUT.PUT_LINE('EMPLOYEE DESIGNATION IS ' || EMP_RECVAR.V_JOB);
DBMS_OUTPUT.PUT_LINE('EMPLOYEE SALARY IS ' || EMP_RECVAR.V_SAL);
END;

I hope now it is clear to you.


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
June 04, 2008 10:14:27   #3  
Muskaan21priya Member Since: June 2008   Contribution: 1    

RE: Function return more than one value
Its depends upon the scenario. as we all knew that function must returns one value and this true. If u want to return more than one value through function you will have to use OUT parameter and record type and REF cursor it will return return more than one value. Please let me know its clear to u or not. I will give explain you by example.
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
June 08, 2008 05:54:36   #4  
KD09714 Member Since: June 2008   Contribution: 11    

RE: Function return more than one value

In addition to all above answers
i wanted to add one more clarifications that

we should not use OUT and IN OUT parameter in the functions supposed to be called from sql expressions. that's where the people used to say we cant use out parameter in functions (called from sql expressions).

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
August 07, 2008 05:09:10   #5  
gtomar Member Since: July 2008   Contribution: 9    

RE: Function return more than one value

Hi

First of all I want to make it clear that function must return a value but also can return more than one value at a time.

There are two ways to return more than one value from function:-

1) you can implement this by passing out parameters in function. If you are using out parameter then you can not excute function through select statement.

for ex:-

Create or replace function func1
(id1 IN <datatype1>
id2 OUT <datatype1>
id3 OUT <datatype1>
id4 OUT <datatype1>
id5 OUT <datatype1>
id6 OUT <datatype1>
.....
)
return <datatype>
AS

BEGIN
--body of procedure

END func1;


Calling function from any block

BEGIN
.....
var1 func1(var2 var3 var4 var5 var6 var7 ......);
....
END;


2) Other way is to use reference cursor .

for ex :-

DECLARE
A NUMBER : 10;
B NUMBER ;
C NUMBER ;
D NUMBER ;
FUNCTION TEST_FUN (A IN NUMBER B OUT NUMBER C IN OUT NUMBER) RETURN NUMBER IS
D NUMBER;
BEGIN
B : A;
C : B + A;
D : A + B + C;
RETURN (D);
END ;
BEGIN
D : TEST_FUN(A B C);
DBMS_OUTPUT.PUT_LINE(' A ' || A);
DBMS_OUTPUT.PUT_LINE(' B ' || B);
DBMS_OUTPUT.PUT_LINE(' C ' || C);
DBMS_OUTPUT.PUT_LINE(' D ' || D);
END;


Using REF CURSOR we can return objects as return type. Here is example


DECLARE
TYPE EMP_REFCUR IS REF CURSOR;
EMP_CURVAR EMP_REFCUR;
TYPE EMP_REC IS RECORD
(
V_EMPNO EMP.EMPNO TYPE
V_ENAME EMP.ENAME TYPE
V_JOB EMP.JOB TYPE
V_SAL EMP.SAL TYPE
);
EMP_RECVAR EMP_REC;
FUNCTION TEST_FUN (V_EMPNO IN EMP.EMPNO TYPE) RETURN EMP_REFCUR IS
BEGIN
OPEN EMP_CURVAR FOR SELECT EMPNO ENAME JOB SAL FROM EMP WHERE EMPNO V_EMPNO;
RETURN(EMP_CURVAR);
END ;
BEGIN
EMP_CURVAR : TEST_FUN(7369);
FETCH EMP_CURVAR INTO EMP_RECVAR;
DBMS_OUTPUT.PUT_LINE('EMPLOYEE NAME IS ' || EMP_RECVAR.V_ENAME);
DBMS_OUTPUT.PUT_LINE('EMPLOYEE DESIGNATION IS ' || EMP_RECVAR.V_JOB);
DBMS_OUTPUT.PUT_LINE('EMPLOYEE SALARY IS ' || EMP_RECVAR.V_SAL);
END;

Thanks
Gunjan


 
Is this answer useful? Yes | No
March 09, 2009 16:07:38   #6  
ananth.oracle Member Since: March 2009   Contribution: 3    

RE: Function return more than one value
A Function can return one value at a time. If you want to return more than one value at a time from function then you should be using table functions.
 
Is this answer useful? Yes | No
July 08, 2009 15:04:38   #7  
javedans Member Since: January 2009   Contribution: 8    

RE: Function return more than one value
Its a universal truth that function can return only one value but there are some tricks by using that you can get more than one value while function returning only one value.

When you define function then you must define type of return statement like Varchar2 Number or Date

CREATE OR REPLACE FUNCTION A RETURN NUMBER;

Similar way if you define return type any existing record type or rowtype then while returning one value you can get more one value as output.

CREATE OR REPLACE FUNCTION A RETURN emp rowtype;

in this case you return rentire row of emp table which may contain any number of column.

 
Is this answer useful? Yes | No
July 23, 2009 02:26:00   #8  
sen_sam86 Member Since: June 2008   Contribution: 10    

RE: Function return more than one value
Yes this can be possible by TABLE FUNCTION

STEP - 1
CREATE TYPE ex_tabl_fun_obj AS OBJECT (emp_name varchar2(20) emp_id NUMBER(2));

STEP - 2

CREATE TYPE ex_tabl_fun_tbl AS TABLE OF ex_tabl_fun_obj;

STEP - 3

CREATE FUNCTION get_all_objects (p_emp_sal NUMBER)
RETURN ex_tabl_fun_tbl AS
v_tab ex_tabl_fun_tbl : ex_tabl_fun_tbl();

BEGIN

FOR i in (SELECT emp_sal emp_id FROM emp_tabl WHERE emp_sal > emp_sal)

LOOP
v_tab EXTEND;
v_tab(v_tab.LAST) : ex_tabl_fun_obj(i.emp_sal i.emp_id);
END LOOP;
RETURN v_tab;

END;

hope this is helpful

thank
senthil






 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape