BC remote Oracle DBA - Call (800) 766-1884
Free Oracle Tips

Oracle Consulting Oracle Training Development

Remote DBA

 

Remote DBA Plans
Remote DBA Service

 
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 Internals Magazine
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





   

 

 

   
  Oracle Tips by Burleson

Calling Functions from PL/SQL

In a PL/SQL block, a function can be called in a SQL statement (as seen above) or used in a simple assignment operation.

SQL> declare
  2    v_far  number := &Farenheit;
  3    v_cels number := 0;
  4  begin
  5    v_cels := f2c(v_far);
  6    dbms_output.put_line(
                'Degrees Celcius is '|| f2c(v_far));
  7  end;
  8  / 

Enter value for farenheit: 46 

old   2:   v_far  number := &Farenheit;
new   2:   v_far  number := 46; 

Degrees Celcius is 7.77777777777777777777777777777777777778 

Functions can also be used to validate variables.  In the example below the value of n_test is validated before additional processing.

SQL> create or replace function valid_numb
  2    (n_numb IN number)
  3    return boolean
  4  as
  5  begin
  6    if n_numb < 10 then return true;
  7    else return false;
  8    end if;
  9  end;
 10  / 

Function created.

SQL> declare
  2    n_test number := &Test;
  3  begin
  4    if (valid_numb(n_test))
  5      then dbms_output.put_line('Valid');
  6    else dbms_output.put_line('Invalid');
  7    end if;
  8  end;
  9  / 

Enter value for test: 8 

old   2:   n_test number := &Test;
new   2:   n_test number := 8; 

Valid 

PL/SQL procedure successfully completed. 

SQL> / 

Enter value for test: 12

old   2:   n_test number := &Test;
new   2:   n_test number := 12; 

Invalid 

PL/SQL procedure successfully completed.

This is a simple test to determine if the number is less than 10.  The function valid_numb returns a type Boolean, so it can be used as the condition of a comparison operation.  Line 4 calls the function in order to get a true/false value which is used to control the program execution. 

When should you use a procedure or a function?  Since most modules can be written either as a procedure or a function, the decision between them comes down to efficiency and maintainability.  Functions are preferred when a module returns one value.  This value may be a variable, record or array, but as long as the module always returns one value, it is a good candidate for a procedure. 


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

Remote DBA Service
 

Oracle Tuning Book

Free Oracle dictionary reference poster

BC Oracle support

Oracle books by Rampant

Oracle monitoring software

North Carolina Oracle Users Group

 

 Arabian horse breeder

Seeing eye horses

 

 

Burleson is the American Team

American Flag

 

 

BC Remote Oracle Support
P.O. Box 511 • Kittrell, NC, 27544

Remote DBA

Remote DBA Services

 

Copyright © 1996 -  2011 by Burleson Enterprises. All rights reserved.

Oracle® is the registered trademark of Oracle Corporation.