Professional Documents
Culture Documents
PLSQL Day 5 Loops
PLSQL Day 5 Loops
******
Loops are useful to execute set of stmts repeatedly.
In oracle , we have 3 types of loops.
i) simple Loop
ii) For Loop
iii) While Loop
1) simple loop
************
syn: LOOP
stmt;
stmt;
:
EXIT WHEN(condition);
stmt;
stmt;
:
END LOOP;
2) FOR LOOP
***********
syntax:
Ex:
begin
for x in 1..5
loop
dbms_output.put_line(x);
end loop;
end;
output:
1
2
3
4
5
Ex:
begin
for x in REVERSE 1..5
loop
dbms_output.put_line(x);
end loop;
end;
OUTPUT:
5
4
3
2
1
3) WHILE LOOP
*************
syntax:
WHILE ( cond )
LOOP
stmt;
stmt;
:
END LOOP;
Ex:
write a procedure to display the sequential of values from
1 to 10 using above loops?
x int:=1;
begin
--x:=1;
dbms_output.put_line(' By using simple loop');
dbms_output.put_line('-----------------------');
loop
dbms_output.put_line(x);
x:=x+1;
exit when(x > 10);
end loop;
DBMS_OUTPUT.PUT_LINE(' END OF SIMPLE LOOP');
DBMS_OUTPUT.PUT_LINE(' ');
dbms_output.put_line('-----------------------------------------------------------')
;
while(x<=10)
loop
dbms_output.put_line(x);
x:=x+1;
end loop;
Ex:
write a procedure to display even numbers from given number
to given number?
Ex:
write a procedure to insert radius, pi value and area values
into the table "circle" ,for the radius range 1 to 20?
sample output:
exec proc_circle_area;
10 rows selected
Ex:
write a procedure to display reverse number for the given
number?
Ex:
CASE statement:
**************
The CASE statement begins with the keyword CASE.
The keyword is followed by a selector.
The selector is followed by one or more WHEN clauses,
which are checked sequentially.
The value of the selector determines which clause is executed.
If the value of the selector equals the value of a WHEN-clause expression,
that WHEN clause is executed.
Syntax:
---------
CASE selector
WHEN expression1 THEN
sequence_of_statements1;
...
WHEN expression2 THEN
sequence_of_statements2;
...
WHEN expressionN THEN
sequence_of_statementsN;
...
[ELSE
sequence_of_statements]
END CASE;
Example:
write a program to display meaning of a given grade from A/B/C/D/F?
declare
grade char;
begin
grade:='&grade';
CASE grade
WHEN 'A' THEN
dbms_output.put_line('Excellent');
WHEN 'B' THEN
dbms_output.put_line('Very Good');
WHEN 'C' THEN
dbms_output.put_line('Good');
WHEN 'D' THEN
dbms_output.put_line('Fair');
WHEN 'F' THEN
dbms_output.put_line('Poor');
END CASE;
exception
when case_not_found then
dbms_output.put_line
(' Invalid grade value entered,
Enter any grade value from A B C D F');
END;
PL/SQL also provides a searched CASE statement, which has the form:
CASE
WHEN search_condition1 THEN sequence_of_statements1;
WHEN search_condition2 THEN sequence_of_statements2;
...
WHEN search_conditionN THEN sequence_of_statementsN;
[ELSE sequence_of_statementsN+1;]
END CASE;
CASE
WHEN grade = 'A' THEN dbms_output.put_line('Excellent');
WHEN grade = 'B' THEN dbms_output.put_line('Very Good');
WHEN grade = 'C' THEN dbms_output.put_line('Good');
WHEN grade = 'D' THEN dbms_output.put_line('Fair');
WHEN grade = 'F' THEN dbms_output.put_line('Poor');
ELSE dbms_output.put_line('No such grade');
END CASE;
Ex:
write a procedure to return emp job grade based on his salary range
from given empid?
declare
ch int;
cnt int;
begin
dbms_output.put_line(' Select your choice from below ');
dbms_output.put_line('--------------------------------');
dbms_output.put_line('Enter 1 for Mobile availability');
dbms_output.put_line('Enter 2 for Smart Phone availability');
dbms_output.put_line('Enter 3 for laptop availability');
ch:=&ch;
proc_prod_count(ch,cnt);
dbms_output.put_line(' selected choice='||ch);
dbms_output.put_line(' Available count= '||cnt);
end;
-----------------------------------------------------------------------------------
-----
--> write a procedure to calculate final bill amount
based on given prod code and qty
and calculate discount on the total bill ,based on below range