BC remote Oracle DBA - Call (800) 766-1884  
Oracle Consulting Oracle Training Development

Remote DBA

Remote DBA Plans  

Remote DBA Service

Remote DBA RAC

   
Remote DBA Oracle Home
Remote DBA Oracle Training
Remote DBA SQL Tuning Consulting
Remote DBA Oracle Tuning Consulting
Remote DBA Data Warehouse Consulting
Remote DBA Oracle Project Management
Remote DBA Oracle Security Assessment
Remote DBA Unix Consulting
Burleson Books
Burleson Articles
Burleson Web Courses
Burleson Qualifications
Oracle Links
Remote DBA Oracle Monitoring
Remote DBA Support Benefits
Remote DBA Plans & Prices
Our Automation Strategy
What We Monitor
Oracle Apps Support
Print Our Brochure
Contact Us (e-mail)
Oracle Job Opportunities
Oracle Consulting Prices





   

 

 

 

Remote DBA services

Remote DBA Support

Remote DBA RAC

Remote DBA Reasons

Remote Oracle Tuning

Remote DBA Links

Oracle DBA Support

Oracle DBA Forum

Oracle Disaster

Oracle Training

Oracle Tuning

Oracle Training

 Remote DBA SQL Server

Remote MSSQL Consulting

Oracle DBA Hosting

Oracle License Negotiation

 

 


 

 

 

 

 

 
 

RETURN Values in PL/SQL

Oracle Tips by Burleson Consulting

RETURN Values in PL/SQL

 As stated above, a function must return a value of the datatype defined in the header.  The value can be a literal, a variable or a calculated value.  The actual value returned is defined in the RETURN clause XE "RETURN clause" .

create or replace function full_name
   (v_fname IN author.author_first_name%type,
    v_lname IN author.author_last_name%type)
   return varchar2
as
begin
   return initcap(v_fname||' '||v_lname);
end;
/

The example above concatenates two variables and passes the result through Oracle’s initcap function XE "initcap function"  before returning the final results.  A function can have multiple RETURN clauses but will terminate the function upon executing the first RETURN clause XE "RETURN clause" .  In the example below, the CASE statement will insure that one of the four RETURN clauses is executed.

SQL> create or replace function num2word
2    (n_number IN number)  
3    return varchar2  
4  as  
5  begin  
6    case n_number  
7      when 1 then return 'one';  
8      when 2 then return 'two';  
9      when 3 then return 'three';  
10    else return 'greater than 3';  
11    end case;  
12  end;  
13  /

Function created.

SQL> begin  
2    for i in 1 .. 4 loop  
3      dbms_output.put_line(num2word(i));  
4    end loop;  
5  end;  
6  / one two three greater than 3

PL/SQL procedure successfully completed.

If an IF/THEN statement is used, you may not reach a RETURN statement XE "RETURN statement"  and the function will raise an exception.

SQL> create or replace function num2word  
2    (n_number IN number)  
3    return varchar2  
4  as  
5  begin  
6    if n_number = 1  
7      then return 'one';  
8    elsif n_number = 2 then return 'two';  
9    elsif n_number = 3 then return 'three';  
10    end if;  
11  end;  
12  / 


Function created.

SQL> begin  
2    for i in 1 .. 4 loop  
3      dbms_output.put_line(num2word(i));  
4    end loop;  
5  end;  
6  /
one
two
three
begin
*
ERROR at line 1:
ORA-06503: PL/SQL: Function returned without value
ORA-06512: at "PUBS.NUM2WORD", line 9
ORA-06512: at line 3

Everything worked as expected except that when we passed a value greater than 3, the function ends without executing the RETURN clause XE "RETURN clause".  In this example, passing a too-high value causes the function to raise an exception.  To avoid this error, we add a RETURN clause at the end of the function to catch any missed values.  One of the most common causes of not executing a RETURN clause is due to the function raising an exception.  If the function has an exception handling section (discussed next in this Chapter) the exception handler must contain a RETURN clause or it will simply raise another exception. Remember, unlike a procedure, a function is never executed independently and it must be called to execute.

The above book excerpt is from:

Easy Oracle PL/SQL Programming

Get Started Fast with Working PL/SQL Code Examples

ISBN 0-9759135-7-3   

John Garmany 

http://www.rampant-books.com/book_2005_1_easy_plsql.htm

If you like Oracle tuning, you may enjoy the book Oracle Tuning: The Definitive Reference , with over 900 pages of BC's favorite tuning tips & scripts. 

You can buy it directly from the publisher and save 30%, and get instant access to the code depot of Oracle tuning scripts.

Expert Remote DBA

BC is America's oldest and largest Remote DBA Oracle support provider.  Get real Remote DBA experts, call
BC Remote DBA today.

 

 

Remote DBA Service
 

Oracle Tuning Book

Advance SQL Tuning Book 

BC Oracle support

Oracle books by Rampant

Oracle monitoring software

 

 

 

 

 

 

BC Remote Oracle Support

Remote DBA

Remote DBA Services

Copyright © 1996 -  2013 by Burleson. All rights reserved.

Oracle® is the registered trademark of Oracle Corporation.



Hit Counter