Scope of the LOOP Counter Variable
The loop counter is defined only within the loop. You cannot reference that variable name outside the loop. After the loop exits, the loop counter is undefined:
You do not need to declare the loop counter because it is implicitly declared as a local variable of type INTEGER. It is safest not to use the name of an existing variable, because the local declaration hides any global declaration.
BEGIN
FOR i IN 1.. 3 LOOP -- assign the values 1,2,3 to i
dbms_output.Put_line(To_char(i));
END LOOP;
dbms_output.Put_line(To_char(i)); -- raises an error
END;
/
You do not need to declare the loop counter because it is implicitly declared as a local variable of type INTEGER. It is safest not to use the name of an existing variable, because the local declaration hides any global declaration.
DECLARE
i NUMBER := 5;
BEGIN
FOR i IN 1.. 3 LOOP -- assign the values 1,2,3 to i
dbms_output.Put_line(To_char(i));
END LOOP;
dbms_output.Put_line(To_char(i)); -- refers to original variable value (5)
END;
/
0 comments:
Post a Comment