22. 1. 1. Conditional Logic |
|
You may use the IF, THEN, ELSE, ELSIF, and END IF keywords in PL/SQL for performing conditional logic. |
The following syntax illustrates the use of conditional logic: |
IF condition1 THEN
statements1
ELSIF condition2 THEN
statements2
ELSE
statements3
END IF;
|
|
where |
- condition1 and condition2 are Boolean expressions that evaluate to true or false.
- statements1, statements2, and statements3 are PL/SQL statements.
|
This conditional logic flows as follows: |
- If condition1 is true, then statements1 is executed.
- If condition1 is false but condition2 is true, then statements2 is executed.
- If neither condition1 nor condition2 are true, then statements3 is executed.
|
SQL> CREATE OR REPLACE PROCEDURE cant_go_there
2 AS
3 l_salary NUMBER := 10000;
4 BEGIN
5 IF l_salary > 20000
6 THEN
7 dbms_output.put_line ('Executive');
8 ELSE
9 dbms_output.put_line ('Drone');
10 END IF;
11 END cant_go_there;
12 /
Procedure created.
SQL>
SQL> SHOW ERRORS
No errors.
SQL>
|
|
SQL> CREATE OR REPLACE PROCEDURE cant_go_there
2 AS
3 l_name varchar2(100) := 'steven';
4 BEGIN
5 IF l_name = 'STEVEN'
6 THEN
7 dbms_output.put_line ('Impossible');
8 ELSE
9 dbms_output.put_line ('Guaranteed');
10 END IF;
11 END cant_go_there;
12 /
Procedure created.
SQL>
SQL> SHOW ERRORS
No errors.
SQL>
|
|