Documente Academic
Documente Profesional
Documente Cultură
♦ Conditional Control
♦ Iterative Control
♦ Sequential Control
Conditional Control
It allows testing the truth of a condition and executing
sections of program depending on the condition that may be
true or false.
Iterative Control
It allows executing a section of program repeatedly as long
as a specified condition remains true.
Sequential Control
It allows ordering the sequence of processing sections of
program.
Conditional Control
IF a > b THEN
dbms_ouput.put_line(‘a is greater’);
END IF;
IF-THEN-ELSE
IF condition THEN
sequence_of_statements1;
ELSE
sequence_of_statements2;
END IF;
Illustration of IF-ELSE construct PL/SQL block to find greater of two
numbers. User will enter any two numbers and IF statement will check for
the greater among the entered numbers.
DECLARE
A NUMBER := &ENTER_A;
B NUMBER := &ENTER_B;
BEGIN
IF A > B THEN
DBMS_OUTPUT.PUT_LINE(‘ A IS GREATER ‘);
ELSE
DBMS_OUTPUT.PUT_LINE(‘ B IS GREATER ‘);
END IF;
END;
Illustration of Nested If-Else construct, PL/SQL block to find greatest of three numbers.
DECLARE
A NUMBER := &ENTER_A;
B NUMBER := &ENTER_B;
C NUMBER := &ENTER_C;
BEGIN
IF A>B THEN
IF A>C THEN
DBMS_OUPUT.PUT_LINE(‘A IS GREATEST’);
ELSE
DBMS_OUPUT.PUT_LINE(‘C IS GREATEST’);
END IF;
ELSE
IF B>C THEN
DBMS_OUPUT.PUT_LINE(‘B IS GREATEST’);
ELSE
DBMS_OUPUT.PUT_LINE(‘C IS GREATEST’);
END IF;
END IF;
END;
IF-THEN-ELSIF
IF condition1 THEN
sequence_of_statements1;
ELSIF condition2 THEN
sequence_of_statements2;
ELSE
sequence_of_statements3;
END IF;
Illustration of IF-THEN-ELSIF, PL/SQL block to calculate addition, subtraction,
multiplication and division of two numbers according to user choice.
DECLARE
A NUMBER=&A;
B NUMBER:=&B;
C NUMBER;
X NUMBER;
BEGN
X:=&ENTER_CHOICE;
IF X=1 THEN
C:=A+B;
ELSIF X=2 THEN
C:=A-B;
ELSIF X=2 THEN
C:=A-B;
ELSIF X=3 THEN
C:=A*B;
ELSIF X=4 THEN
C:=A/B;
ELSE
DBMS_OUTPUT.PUT_LINE(‘NOT A VALID OPTION’);
END IF;
DBMS_OUTPUT.PUT_LINE(‘RESULT IS’ || C);
END;
Illustration of IF-THEN-ELSEIF, PL/SQL block that will print message
according to time when we execute the PL/SQL block
DECLARE
TIME_OF_EXECUTION VARCHAR2(10) ;
BEGIN
TIME_OF_EXECUTION := TO_CHAR(SYSDATE,'HH24');
IF TIME_OF_EXECUTION >=0 AND
TIME_OF_EXECUTION <12 THEN
DBMS_OUTPUT.PUT_LINE(' GOOD MORNING ');
ELSIF TIME_OF_EXECUTION >12 AND TIME_OF_EXECUTION <=17
THEN
DBMS_OUTPUT.PUT_LINE(' GOOD AFTER NOON ');
ELSIF TIME_OF_EXECUTION >17 AND TIME_OF_EXECUTION <20
THEN
DBMS_OUTPUT.PUT_LINE(' GOOD EVENING ');
ELSE
DBMS_OUTPUT.PUT_LINE(' GOOD NIGHT ');
END IF;
END;
Logical Condition Building
Any expression containing a null value evaluates to NULL,
with the exception of a concatenated expression, which
treats the null value as an empty string.
Examples:
Both these expressions evaluate to NULL if v_sal is NULL.
v_sal>1000;
v_sal*1.1
Iterative Control
done := FALSE;
WHILE NOT done
LOOP
sequence_of_statements;
done := boolean_expression;
END LOOP;
A statement inside the loop must assign a new value to the Boolean
variable. Otherwise, you have an infinite loop.
For example, the following LOOP statements are logically
equivalent:
WHILE TRUE LOOP | LOOP ... |
...END LOOP; | END LOOP;
Illustration of WHILE LOOP, PL/SQL block to print multiplication table of
any number
.
DECLARE
TABLE_OF NUMBER :=&ENTER_TABLEOF ;
COUNT NUMBER := 1;
RESULT NUMBER;
BEGIN
WHILE COUNT <= 10 LOOP
RESULT := TABLE_OF * COUNT ;
DBMS_OUTPUT.PUT_LINE(TABLE_OF||’ * ‘||
COUNT ||’=‘||RESULT);
COUNT := COUNT +1 ;
END LOOP;
END;
Illustration of WHILE LOOP with the use of SQL statements
PL/SQL block to calculate the SIMPLE INTEREST for same
PRINCIPAL and TIME but with different RATE OF
INTEREST starting from 5% and to store each result in a
prefabricated table.
SIMPLE_INTEREST PRINCIPAL RATE TIME SI
DECLARE
PRINCIPAL NUMBER:=&ENTER_P;
RATE_OF_INT NUMBER:=5 ;
TIME_IN_YEAR NUMBER:=2;
SIMPLE_INT NUMBER(6,2);
BEGIN
WHILE RATE_OF_INT < =15
LOOP
SIMPLE_INT := (PRINCIPAL*RATE_OF_INT*TIME_IN_YEAR)/100;
INSERT INTO SIMPLE_INTEREST(PRINCIPAL,RATE,TIME,SI)
VALUES(PRINCIPAL,RATE_OF_INT,TIME_IN_YEAR,SIMPLE_INT);
RATE_OF_INT := RATE_OF_INT + 1 ;
END LOOP;
END;
FOR-LOOP Statement