Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 
 Home
 E-mail Us
 Oracle Articles
New Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

 Applications
 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB  

Don Burleson Blog 


 

 

 


 

 

 

 
 

Oracle Concepts - PL/SQL IF/THEN/ELSE Statements

Oracle Tips by Burleson Consulting

PL/SQL IF/THEN/ELSE Statements

The IF/THEN statement checks a Boolean value or expression and if true, executes the statements in the THEN clause.  If the condition is false, the statements in the THEN clause are skipped and execution jumps to the END IF, and the expression that is checked must return a true or false.  It can be a simple Boolean variable or a compound expression joined with AND/OR clauses, and the expression can even be a PL/SQL function that returns a Boolean value. 

There is no requirement to surround the expression with parenthesis but most developers use parentheses for clarity.  The THEN clause can contain a single or multiple statements, or a nested PL/SQL block.  Here is an example of a basic IF/THEN statement with a THEN clause.

if v_numb > 5 then

  v_numb  := 5;

  v_other := 10;

end if;

In the statements above, the Boolean condition (v_numb > 5) must be true before the THEN clause is executed.  If v_numb is equal to or less than 5, or if it evaluates to NULL, the program control jumps to the statement after the END IF clause.  Note that the THEN clause can contain any number of valid PL/SQL statements.  The variable v_other and v_numb will not change unless the condition is true. 

Note:  All the IF/THEN statements must end with an END IF clause.  The PL/SQL engine will continue to include statements in the THEN clause until it encounters an END IF.  If you get a compile error that states: ?found xxxx when expecting IF?, the compiler encountered an END statement before it encountered the END IF.  Simply find the end of you IF statement and close it with an END IF to correct the problem.

Sometimes the program flow will want to branch one direction if the condition is true and another direction if the condition is false, and this is handled within the IF/THEN/ELSE statement.

Like the IF/THEN statement, the THEN clause statements will only be executed if the condition is true.  However if the condition is false, the statements in the ELSE clause are executed.  This is an either-or situation.

if n_numb > 5 then
  v_status := ?large?;
else
  v _status := ?small?;
end if;

For the complete story, we recommend the book ?Easy Oracle PL/SQL Programming?.  Once you have mastered basic SQL you are ready for the advanced book ?Oracle PL/SQL Tuning? by Dr. Timothy Hall.

After executing the example, the variable v_status will be defined as either large or small.  Note that the above statement could also be written as two separate IF statements.

if n_numb > 5 then v_status := ?large?; end if;

if n_numb <=5 then v_status := ?small?; end if;

Programmatically, the results are the same, however the two IF statements required two evaluations, while the IF/THEN/ELSE statement requires only one evaluation. 

This is an excerpt from the bestselling "Easy Oracle Jumpstart" by Robert Freeman and Steve Karam (Oracle ACE and Oracle Certified Master).  It?s only $19.95 when you buy it directly from the publisher here.

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

You can buy it direct from the publisher for 30%-off and get instant access to the code depot of Oracle tuning scripts.


 

 

��  
 
 
Oracle Training at Sea
 
 
 
 
oracle dba poster
 

 
Follow us on Twitter 
 
Oracle performance tuning software 
 
Oracle Linux poster
 
 
 

 

Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  

and include the URL for the page.


                    









Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


 

Copyright © 1996 -  2017

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.

Remote Emergency Support provided by Conversational