Quick Search:
CODE
Oracle PL/SQL Code Library
JOBS
Find Or Post Oracle Jobs
FORUM
Oracle Discussion & Chat
 The Oracle PL/SQL IF Keyword      [Return To Index] Jump to:  
  Looking for the original pages? (formerly called "Morgan's Library") You can find them here.

Term: IF

Definition:
The Oracle IF keyword is part of the PL/SQL IF...THEN construct which implements conditional control in PL/SQL. Conditional Control refers to taking an alternative action instead of regular one, based on defined conditions or logic-based decisions. It allows conditional execution of statements in defined sequence. The IF...THEN construct is available in three formats:

  1. IF...THEN
  2. IF...THEN...ELSE
  3. IF...THEN...ELSIF...ENDIF

IF THEN...END IF statement is a single-level conditional statement which executes a sequence of statements based on a given condition:

IF condition THEN
executable statements
END IF;


A plain-English example of this could be, "IF the temperature is below 50 degrees, THEN turn on the heater."

The IF-THEN-ELSE statement is next level of extension of above the IF-THEN statement, as it provides the ability to give direction for FALSE or NULL also.

IF condition THEN
executable statements 1
ELSE
executable statements 2
END IF;


A plain-English example of this could be, "IF the temperature is below 50 degrees, THEN turn on the heater, ELSE open a window."


For multiple possible outcomes of a condition, we use the IF THEN ELSIF ENDIF statement.

IF condition1 THEN
sequence_of_statements1
ELSIF condition2 THEN
sequence_of_statements2
ELSE
sequence_of_statements3
END IF;


Example Usage:

The block of code below looks at the value of the variable 'V_GRADE'. If it is 'P' then it prints out 'PASS'.

DECLARE
V_GRADE VARCHAR2(1) := 'P';
BEGIN
IF V_GRADE = 'P' THEN
DBMS_OUTPUT.PUT_LINE('PASS');
END IF;
END;



Related Links:

Related Code Snippets:
 
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 70 users online    © 2009 psoug.org
PSOUG LOGIN
Username: 
Password: 
Forgot your password?