a series of statements a fixed number of times.
The foll is a syntax for loop statement.
FOR loop_counter IN [REVERSE] lower_bound .. higher_bound
LOOP
sequence_of_statements;
END LOOP;
SQL> declare
2 n1 number;
3 begin
4 for i in 1.. 10
5 loop
6 dbms_output.put_line(i);
7 end loop;
8 end;
9 /
Output :-
1
2
3
4
5
6
7
8
9
10
in the
PL/SQL procedure successfully completed.
in the next example we will use the RESERVE keyword to print a list of integers in
descending order.
declare
n1 number;
begin
for i in reverse 1.. 10
loop
dbms_output.put_line(i);
end loop;
end;
/
Output
SQL> /
10
9
8
7
6
5
4
3
2
1
Example 2. write a PL/SQL block to display names and salary of employees.
.
DECLARE
V_NAME EMPLOYEES.LAST_NAME%TYPE;
V_SAL EMPLOYEES.SALARY%TYPE;
BEGIN
FOR V_ID IN 100..109 LOOP
SELECT LAST_NAME,SALARY
INTO V_NAME,V_SAL
FROM EMPLOYEES
WHERE EMPLOYEE_ID=V_ID;
DBMS_OUTPUT.PUT_LINE(V_NAME||' GETS '||V_SAL);
END LOOP;
END;
Output:-
King GETS 24000
Kochhar GETS 17000
De Haan GETS 17000
Hunold GETS 9000
Ernst GETS 6000
Austin GETS 4800
Pataballa GETS 4800
Lorentz GETS 4200
Greenberg GETS 12000
Faviet GETS 9000
PL/SQL procedure successfully completed.
0 comments:
Post a Comment
Thank you for your comments we will get back to soon