spool file

advertisement
SQL>
SQL>
SQL>
2
3
4
5
6
7
1
2
3
4
5
6
7
8
9
10
--Write an anonymous block to print numbers from 1 to 10 on screen. Use a for loop.
set serveroutput on
begin
for counter in 1 .. 10
loop
dbms_output.put_line(counter);
end loop;
end;
/
PL/SQL procedure successfully completed.
SQL> --Note that we do not need to declare the counter. for loop automatically declares and creates
SQL> --the counter at the beginning of the loop and when the loop exits the counter is destroyed!
SQL> --*****read the following code and state the output expected ************
SQL> ed
Wrote file afiedt.buf
1
2
3
4
5
6
7
8
9
declare
counter number := 3;
begin
dbms_output.put_line('Before the loop counter='||counter);
for counter in 1 .. 10
loop
dbms_output.put_line(counter);
end loop;
dbms_output.put_line('After the loop counter='||counter);
10* end;
SQL> /
Before the loop counter=3
1
2
3
4
5
6
7
8
9
10
After the loop counter=3
PL/SQL procedure successfully completed.
SQL> --In fact there are two variables used in the above program. One variable is the local
SQL> --variable loop declared at the declaration section. This variable has the value 3.
SQL> --when the program uses counter outside the loop (the put_line statements before and after
SQL> --the loop) the local counter variable (with the value 3) is used.
SQL> --When the for loop is invoked, it automatically creates ANOTHER variable with the same name
SQL> --in this example. This NEW counter variable is initialized to 1 and is incremented until it re
SQL> --reaches 10. When the loop exits, this variable is "destroyed" and is no longer available
SQL> --outside the loop!
SQL>
SQL> --Task 2: Print all even numbers between 1 and 10 on screen. Use a for loop.
SQL> --****Note that we canot modify a loop counter in a for loop
SQL> ed
Wrote file afiedt.buf
1
2
3
4
5
6
begin
for counter in 1 .. 10
loop
counter := counter+2;
dbms_output.put_line(counter);
end loop;
7* end;
SQL> /
counter := counter+2;
*
ERROR at line 4:
ORA-06550: line 4, column 5:
PLS-00363: 'COUNTER' cannot be used as an assignment targe
ORA-06550: line 4, column 5:
PL/SQL: Statement ignored
SQL> --As the above message shows the counter variable which is declared by the for loop cannot be used
as an assignment target
SQL>
SQL> --solution 1
SQL> ed
Wrote file afiedt.buf
1 begin
2
for counter in 1 .. 5
3
loop
4
dbms_output.put_line(counter*2);
5
end loop;
6* end;
SQL> /
2
4
6
8
10
PL/SQL procedure successfully completed.
SQL> --notice that we do not modify the value of the counter, we are merely printing twice the counter on
the screen
SQL> --solution 2
SQL> ed
Wrote file afiedt.buf
1 begin
2
for counter in 1 .. 10
3
loop
4
if mod(counter,2)=0 then
5
dbms_output.put_line(counter);
6
end if;
7
end loop;
8* end;
SQL> /
2
4
6
8
10
PL/SQL procedure successfully completed.
SQL> --solution 3
SQL> ed
Wrote file afiedt.buf
1
2
3
4
5
6
7
8
9*
SQL>
2
4
6
8
10
declare
even_nos number :=2;
begin
for counter in 1 .. 5
loop
dbms_output.put_line(even_nos);
even_nos := even_nos+2;
end loop;
end;
/
PL/SQL procedure successfully completed.
SQL> --Task 3: Print the numbers from 10 to 1 on screen. Use a for loop.
SQL> ed
Wrote file afiedt.buf
1 begin
2
for counter in 10 .. 1
3
loop
4
dbms_output.put_line(counter);
5
end loop;
6* end;
SQL> /
PL/SQL procedure successfully completed.
SQL> --the frst limit in a for statement must be less than or equal to the second limit.
SQL> --in this case since 10 (first limit=lower limit) is already greater than 1 (second
SQL> --limit=upper limit) the for loop is never executed
SQL> ed
Wrote file afiedt.buf
1 begin
2
for counter in reverse 1 .. 10
3
loop
4
dbms_output.put_line(counter);
5
end loop;
6* end;
SQL> /
10
9
8
7
6
5
4
3
2
1
PL/SQL procedure successfully completed.
SQL> --as the solution above shows we must use the keyword reverse to count in the reverse order in a for
loop
SQL> --another solution???
SQL> ed
Wrote file afiedt.buf
1
2
3
4
5
6
7
8*
SQL>
10
9
8
7
6
5
4
3
2
1
declare
max_no number :=10;
begin
for counter in 0 .. 9
loop
dbms_output.put_line(max_no-counter);
end loop;
end;
/
PL/SQL procedure successfully completed.
SQL> --Task 4: Write an anonymous block to print numbers from 1 to 10 on screen. Use a while loop
SQL> ed
Wrote file afiedt.buf
declare
2
counter number := 1;
3 begin
4
while counter<=10
5
loop
6
dbms_output.put_line(counter);
7
counter := counter+1;
8
end loop;
9
end;
10 /
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> ed
Wrote file afiedt.buf
1 declare
2
counter number := 1;
3 begin
4
while counter<=10
5
loop
6
dbms_output.put_line(counter);
7
counter := counter+1;
8
end loop;
9* end;
SQL> /
1
2
3
4
5
6
7
8
9
10
PL/SQL procedure successfully completed.
SQL> --for future reference: if you initialize a variable at declaration, any exception/error
SQL> --raised at declaration CANNOT be handled at the exception handling section of the current
SQL> --program
SQL> --For this reason, if you want to handle any error that might occur when assigning values to
SQL> --the variable, you must initialize at executable section
SQL> ed
Wrote file afiedt.buf
1 declare
2
counter number;--if the initialization is performed here, the error CANNOT be handled in the
current program
3 begin
4
counter :=1; --if initialization is performed here, the error CAN be handled in the current
program
5
while counter<=10
6
loop
7
dbms_output.put_line(counter);
8
counter := counter+1;
9
end loop;
10* end;
SQL> /
1
2
3
4
5
6
7
8
9
10
PL/SQL procedure successfully completed.
SQL> --Task 5: Print all even numbers between 1 and 10 on screen. Use a while loop
SQL> ed
Wrote file afiedt.buf
1 declare
2
counter number;
3 begin
4
counter :=2;
5
while counter<=10
6
loop
7
dbms_output.put_line(counter);
8
counter := counter+2;
9
end loop;
10* end;
SQL> /
2
4
6
8
10
PL/SQL procedure successfully completed.
SQL> --Task 6 :Print the numbers from 10 to 1 on screen. Use a while loop
SQL> ed
Wrote file afiedt.buf
1
2
3
4
5
6
7
8
9
declare
counter number;
begin
counter :=10;
while counter>=1
loop
dbms_output.put_line(counter);
counter := counter-1;
end loop;
10* end;
SQL> /
10
9
8
7
6
5
4
3
2
1
PL/SQL procedure successfully completed.
SQL> --Task 7 : Write an anonymous block to print numbers from 1 to 10 on screen. Use a basic loop
SQL> ed
Wrote file afiedt.buf
1 declare
2
counter number;
3 begin
4
counter :=1;
5
loop
6
dbms_output.put_line(counter);
7
exit when counter>=10;
8
counter := counter+1;
9
end loop;
10* end;
SQL> /
1
2
3
4
5
6
7
8
9
10
PL/SQL procedure successfully completed.
SQL> --Task 8 : Print all even numbers between 1 and 10 on screen. Use a basic loop
SQL> ed
Wrote file afiedt.buf
1 declare
2
counter number;
3 begin
4
counter :=2;
5
loop
6
dbms_output.put_line(counter);
7
exit when counter>=10;
8
counter := counter+2;
9
end loop;
10* end;
SQL> /
2
4
6
8
10
PL/SQL procedure successfully completed.
SQL> ed
Wrote file afiedt.buf
1
2
3
4
5
declare
counter number;
begin
counter :=2;
loop
6
dbms_output.put_line(counter);
7
exit when counter>=10;
8
counter := counter+2;
9
end loop;
10* end;
SQL> /
2
4
6
8
10
PL/SQL procedure successfully completed.
SQL> --another solution
SQL> ed
Wrote file afiedt.buf
1 declare
2
counter number;
3 begin
4
counter :=1;
5
loop
6
if mod(counter,2)=0 then
7
dbms_output.put_line(counter);
8
end if;
9
exit when counter>=10;
10
counter := counter+1;
11
end loop;
12* end;
SQL> /
2
4
6
8
10
PL/SQL procedure successfully completed.
SQL> ed
Wrote file afiedt.buf
1 declare
2
counter number;
3 begin
4
counter :=1;
5
loop
6
if mod(counter,2)=0 then
7
dbms_output.put_line(counter);
8
end if;
9
exit when counter>=10;
10
counter := counter+1;
11
end loop;
12* end;
SQL> --Task 9 : Print the numbers from 10 to 1 on screen. Use a basic loop
SQL> ed
Wrote file afiedt.buf
1 declare
2
counter number;
3 begin
4
counter :=10;
5
loop
6
dbms_output.put_line(counter);
7
exit when counter<=1;
8
counter := counter-1;
9
end loop;
10* end;
SQL> /
10
9
8
7
6
5
4
3
2
1
PL/SQL procedure successfully completed.
SQL> --Task 10 : Experiment with exit statement in a for loop
SQL> ed
Wrote file afiedt.buf
1 begin
2
for counter in 1 .. 10
3
loop
4
exit when counter=5;
5
dbms_output.put_line(counter);
6
end loop;
7* end;
SQL> /
1
2
3
4
PL/SQL procedure successfully completed.
SQL> ed
Wrote file afiedt.buf
1 begin
2
for counter in 1 .. 10
3
loop
4
dbms_output.put_line(counter);
5
exit when counter=5;
6
end loop;
7* end;
8
/
1
2
3
4
5
PL/SQL procedure successfully completed.
SQL> ed
Wrote file afiedt.buf
1 begin
2
for counter in 1 .. 10
3
loop
4
exit when counter=5;
5
dbms_output.put_line(counter);
6
exit when counter=5;
7
end loop;
8* end;
SQL> /
1
2
3
4
PL/SQL procedure successfully completed.
SQL> ed
Wrote file afiedt.buf
1
2
3
4
5
6
begin
for counter in 1 .. 10
loop
if counter=5 then
exit;
end if;
7
dbms_output.put_line(counter);
8
end loop;
9* end;
10 /
1
2
3
4
PL/SQL procedure successfully completed.
SQL> --Task 10 : Experiment with exit statement in a while loop
SQL> ed
Wrote file afiedt.buf
1 declare
2
counter number := 1;
3 begin
4
while counter<=10
5
loop
6
exit when counter=5;
7
dbms_output.put_line(counter);
8
counter := counter+1;
9
end loop;
10* end;
SQL> /
1
2
3
4
PL/SQL procedure successfully completed.
SQL> ed
Wrote file afiedt.buf
1
declare
2
counter number := 1;
3 begin
4
while counter<=10
5
loop
6
exit when counter=5;
7
dbms_output.put_line(counter);
8
exit when counter=5;
9
counter := counter+1;
10
exit when counter=5;
11
end loop;
12* end;
SQL> /
1
2
3
4
PL/SQL procedure successfully completed.
SQL> ed
Wrote file afiedt.buf
1 declare
2
counter number := 1;
3 begin
4
while counter<=10
5
loop
6
dbms_output.put_line(counter);
7
exit when counter=5;
8
counter := counter+1;
9
exit when counter=5;
10
end loop;
11* end;
12 /
1
2
3
4
PL/SQL procedure successfully completed.
SQL> ed
Wrote file afiedt.buf
1 declare
2
counter number := 1;
3 begin
4
while counter<=10
5
loop
6
dbms_output.put_line(counter);
7
if counter=5 then
8
exit;
9
end if;
10
counter := counter+1;
11
end loop;
12* end;
13 /
1
2
3
4
5
PL/SQL procedure successfully completed.
SQL> spool off
Download