Details
-
Bug
-
Status: Closed (View Workflow)
-
Critical
-
Resolution: Fixed
-
10.3.9
-
None
-
Centos 7
Description
Release tested: 10.3.9
The following loop did not produce any output. It worked correctly without the REVERSE key word.
SET SQL_MODE=ORACLE; |
delimiter //
|
#
|
DECLARE |
totalprice DECIMAL(12,2):=NULL; |
loop_start INTEGER := 1; |
BEGIN |
FOR idx IN REVERSE loop_start..10 LOOP |
SELECT idx; |
END LOOP; |
END; |
//
|
With the REVERSE key word, switching the lower and upper bound values also worked:
FOR idx IN REVERSE 10..loop_start LOOP
However, this behavior is different from Oracle's documentation below.
---------
If REVERSE is specified:
If upper_bound is less than lower_bound, the statements do not run, and control transfers to the statement after the FOR LOOP statement.
---------
Sorry, can you clarify please?
In the above script you have a REVERSE loop and direct bound order. It performs no iterations and produces no output.
This looks correct.
I just checked all four combinations and I think they give correct results:
Direct loop, direct bound order (3 iterations as expected)
delimiter //
#
DECLARE
BEGIN
//
delimiter ;
+-----+
| idx |
+-----+
| 1 |
+-----+
1 row in set (0.01 sec)
+-----+
| idx |
+-----+
| 2 |
+-----+
1 row in set (0.01 sec)
+-----+
| idx |
+-----+
| 3 |
+-----+
1 row in set (0.01 sec)
Query OK, 0 rows affected (0.00 sec)
Direct loop, reverse bound order (no iterations as expected)
SET SQL_MODE=ORACLE;
delimiter //
#
DECLARE
totalprice DECIMAL(12,2):=NULL;
loop_start INTEGER := 1;
BEGIN
FOR idx IN 3..loop_start LOOP
SELECT idx;
END LOOP;
END;
//
delimiter ;
Query OK, 0 rows affected (0.00 sec)
Reverse loop, reverse bound order (3 iterations as expected)
delimiter //
#
DECLARE
BEGIN
//
delimiter ;
+-----+
| idx |
+-----+
| 3 |
+-----+
1 row in set (0.00 sec)
+-----+
| idx |
+-----+
| 2 |
+-----+
1 row in set (0.00 sec)
+-----+
| idx |
+-----+
| 1 |
+-----+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Reverse loop, direct bound order (no iterations as expected)
delimiter //
#
DECLARE
BEGIN
//
delimiter ;
Query OK, 0 rows affected (0.00 sec)