|
|
| Re: increment by 2 in oracle for loop [message #130721 is a reply to message #130701] |
Tue, 02 August 2005 02:28   |
mchadder
Messages: 224 Registered: May 2005 Location: UK
|
Senior Member |
|
|
There's no way of doing this (unlike some languages) of specifying the increment amount in the FOR loop, you'd have to implement your own mechanism, using either LOOP or WHILE.
SQL> DECLARE
2 curr_val PLS_INTEGER;
3 lower_bound PLS_INTEGER := 0;
4 upper_bound PLS_INTEGER := 10;
5 increment PLS_INTEGER := 2;
6 BEGIN
7 curr_val := lower_bound;
8 LOOP
9 IF curr_val <= upper_bound
10 THEN
11 dbms_output.put_line('curr_val : ' || curr_val);
12 ELSE
13 EXIT;
14 END IF;
15 curr_val := curr_val + increment;
16 END LOOP;
17 END;
18 /
curr_val : 0
curr_val : 2
curr_val : 4
curr_val : 6
curr_val : 8
curr_val : 10
PL/SQL procedure successfully completed.
|
|
|
|
|
|
| Re: increment by 2 in oracle for loop [message #130754 is a reply to message #130728] |
Tue, 02 August 2005 05:12  |
JSI2001
Messages: 1016 Registered: March 2005 Location: Scotland
|
Senior Member |
|
|
One way is to use arithmetic
SQL> Begin
2 for i IN 1..10 loop
3 dbms_output.put_line('- iteration '||i||' value '||i*2);--increment by 2
4 dbms_output.put_line('- iteration '||i||' value '||i*3);--increment by 3
5 dbms_output.put_line('- iteration '||i||' value '||i*10);--increment by 10
6 dbms_output.put_line('__________________________________');
7 end loop;
8 end;
9 /
- iteration 1 value 2
- iteration 1 value 3
- iteration 1 value 10
__________________________________
- iteration 2 value 4
- iteration 2 value 6
- iteration 2 value 20
__________________________________
- iteration 3 value 6
- iteration 3 value 9
- iteration 3 value 30
__________________________________
- iteration 4 value 8
- iteration 4 value 12
- iteration 4 value 40
__________________________________
- iteration 5 value 10
- iteration 5 value 15
- iteration 5 value 50
__________________________________
- iteration 6 value 12
- iteration 6 value 18
- iteration 6 value 60
__________________________________
- iteration 7 value 14
- iteration 7 value 21
- iteration 7 value 70
__________________________________
- iteration 8 value 16
- iteration 8 value 24
- iteration 8 value 80
__________________________________
- iteration 9 value 18
- iteration 9 value 27
- iteration 9 value 90
__________________________________
- iteration 10 value 20
- iteration 10 value 30
- iteration 10 value 100
__________________________________
PL/SQL procedure successfully completed.
HTH
Jim
|
|
|
|