Home » SQL & PL/SQL » SQL & PL/SQL » increment by 2 in oracle for loop
icon5.gif  increment by 2 in oracle for loop [message #130701] Tue, 02 August 2005 01:15 Go to next message
bhoite_amol83
Messages: 110
Registered: June 2005
Location: Pune
Senior Member
How to increment in oracle for loop by 2/3/4...

Waiting for reply.
Thanx in advance.
AMol
Re: increment by 2 in oracle for loop [message #130721 is a reply to message #130701] Tue, 02 August 2005 02:28 Go to previous messageGo to next message
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 #130728 is a reply to message #130721] Tue, 02 August 2005 03:03 Go to previous messageGo to next message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
The oracle documentation would have told you the same:
From "PL/SQL User's Guide and Reference Release 2 (9.2) Part Number A96624-01"

Some languages provide a STEP clause, which lets you specify a different increment (5 instead of 1 for example). PL/SQL has no such structure, but you can easily build one.


MHE
Re: increment by 2 in oracle for loop [message #130754 is a reply to message #130728] Tue, 02 August 2005 05:12 Go to previous message
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
Previous Topic: UTL_FILE and Directory
Next Topic: How to query a long in a string?
Goto Forum:
  


Current Time: Fri May 16 08:11:19 CDT 2025