Home » SQL & PL/SQL » SQL & PL/SQL » how to write return in the function
how to write return in the function [message #626280] Fri, 24 October 2014 03:43 Go to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Hi all,
Is this correct process to write return in the function? no need to write Return after End if right?
function xxc_qty_value return Number 
is
  v_qty number(10);
begin

 if ... then  
    
    if ... then  
       begin
            select col1
            into v_qty
            from table1;
          return nvl(v_qty, 0);  
        Exception
            When others then
              v_qty := 0;
              return v_qty;  
        end;
    end if;
    if ... then  
       begin
            select col1
            into v_qty
            from table1;
          return nvl(v_qty, 0);  
        Exception
            When others then
              v_qty := 0;
              return v_qty;  
        end;
    end if;

    else

        Begin
              select col1
            into v_qty
            from table1;
          return nvl(v_qty, 0);  
        Exception
            When others then
              v_qty := 0;
              return v_qty;  
        end;
 end if;  -->Here!!!!!
end;  
Re: how to write return in the function [message #626281 is a reply to message #626280] Fri, 24 October 2014 03:45 Go to previous messageGo to next message
Littlefoot
Messages: 21807
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Quote:
no need to write Return after End if right?

Wrong. If the first IF returns FALSE, a function will terminate without returning a value which is an error.

[Updated on: Fri, 24 October 2014 03:46]

Report message to a moderator

Re: how to write return in the function [message #626282 is a reply to message #626280] Fri, 24 October 2014 03:46 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
when ZERO is returned by this function, what exactly does it mean to the invoking code?

why does function have no input parameter/argument?
why do you need function if it always returns the same results?
Re: how to write return in the function [message #626283 is a reply to message #626281] Fri, 24 October 2014 03:48 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Already written in all the block right? then why to use after End if?
Re: how to write return in the function [message #626285 is a reply to message #626281] Fri, 24 October 2014 03:51 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:
Wrong. If the first IF returns FALSE, a function will terminate without returning a value which is an error.

So must use return after End if , right?
 end if;  
 return nvl(v_qty,0); --> This!!
end; 
Re: how to write return in the function [message #626287 is a reply to message #626285] Fri, 24 October 2014 04:00 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
What is the downside to adding the extra line of code?
Re: how to write return in the function [message #626288 is a reply to message #626287] Fri, 24 October 2014 04:05 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Hi,
What is the correct process?
Quote:
Wrong. If the first IF returns FALSE, a function will terminate without returning a value which is an error.

The Littlefoot post is gives me correct answer.. Smile
Re: how to write return in the function [message #626306 is a reply to message #626288] Fri, 24 October 2014 09:45 Go to previous message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Another correct answer is you can the code of your function by the equivalent one:
function xxc_qty_value return Number 
is
begin
  return 0;
end;
/


[Updated on: Fri, 24 October 2014 09:45]

Report message to a moderator

Previous Topic: Insert Hangs
Next Topic: REMOVE DUPLICATES IN ROWS TO COLUMN
Goto Forum:
  


Current Time: Thu Apr 18 22:35:03 CDT 2024