§ Oracle Compatibility - Stored Procedure - FOR LOOP
§ 1. Syntax
FOR var IN [REVERSE] expr1 .. expr2 LOOP .. END LOOP
-- OR
FOR var IN [REVERSE] expr1..expr2 LOOP .. END LOOP
2
3
4
§ 2. Definition and Usage
GreatSQL supports using the FOR .. IN expr1 .. expr2 LOOP .. END LOOP syntax to read data in a loop and assign it to the corresponding variable. In FOR ... LOOP, the two parameter expressions at the front and back can be connected with the middle dots, for example: FOR .. IN expr1..expr2 LOOP; or connected with only one parameter, for example: FOR .. IN expr1.. expr2 LOOP and FOR .. IN expr1 ..expr2 LOOP are both allowed.
§ 3. Oracle Compatibility Notes
In ORACLE mode, GreatSQL stored procedures support the FOR .. LOOP usage. The usage is as follows:
The variable
varinFOR LOOPdoes not need to be declared in advance; theFOR var IN expr1 .. expr2 LOOPsyntax can be used directly.The
REVERSEkeyword can be added to obtain data in reverse order.expr1/expr2in the loop support numeric-type variables, numbers, functions, expressions, etc. Ifexpr1/expr2is ofFLOATtype, it is converted toINTtype (rounded during conversion).If
expr1/expr2is of a time type, it is converted to a numeric type for calculation instead of being added or subtracted according to time rules. Oracle does not support this type and reports an error.In the loop
FOR var IN, if the variablevaris ofFLOATtype, it is converted toINTtype (rounded during conversion).
§ 4. Examples
- Example 1
greatsql> SET sql_mode = ORACLE;
greatsql> DELIMITER //
greatsql> CREATE OR REPLACE PROCEDURE p1(i FLOAT, j FLOAT) AS
BEGIN
FOR x IN i .. j LOOP
SELECT x;
END LOOP;
END; //
greatsql> CALL p1(1.5, 3.4) //
+------+
| x |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
+------+
| x |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
greatsql> CALL p1(1.4,3.5) //
+------+
| x |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
+------+
| x |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
+------+
| x |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
+------+
| x |
+------+
| 4 |
+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
greatsql> CALL p1(1.5, 3.4) //
+------+
| x |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
+------+
| x |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
It can be seen that when the passed parameter is of FLOAT type, it is converted to INT type (rounded during conversion).
- Example 2
greatsql> SET sql_mode = ORACLE;
greatsql> DELIMITER //
greatsql> CREATE OR REPLACE PROCEDURE p1(i FLOAT, j FLOAT) AS
BEGIN
-- Reverse order
FOR x IN REVERSE i .. j LOOP
SELECT x;
END LOOP;
END; //
greatsql> CALL p1(1.5, 3.4) //
+------+
| x |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
+------+
| x |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
greatsql> CALL p1(1.4, 3.5) //
+------+
| x |
+------+
| 4 |
+------+
1 row in set (0.00 sec)
+------+
| x |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
+------+
| x |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
+------+
| x |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
greatsql> CALL p1(1.5, 3.4) //
+------+
| x |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
+------+
| x |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
- Example 3
greatsql> SET sql_mode = ORACLE;
greatsql> DELIMITER //
greatsql> CREATE OR REPLACE PROCEDURE p1(i FLOAT, j FLOAT) AS
val FLOAT := 1.2;
BEGIN
FOR x IN i .. j LOOP
-- Add a FLOAT type value inside the loop
val := val + x;
SELECT val;
END LOOP;
END; //
greatsql> CALL p1(1.5, 3.4) //
+------+
| val |
+------+
| 3.2 |
+------+
1 row in set (0.00 sec)
+------+
| val |
+------+
| 6.2 |
+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
greatsql> CALL p1(1.4, 3.5) //
+------+
| val |
+------+
| 2.2 |
+------+
1 row in set (0.00 sec)
+------+
| val |
+------+
| 4.2 |
+------+
1 row in set (0.00 sec)
+------+
| val |
+------+
| 7.2 |
+------+
1 row in set (0.01 sec)
+------+
| val |
+------+
| 11.2 |
+------+
1 row in set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
greatsql> CALL p1(1.5, 3.4) //
+------+
| val |
+------+
| 3.2 |
+------+
1 row in set (0.00 sec)
+------+
| val |
+------+
| 6.2 |
+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
Scan the QR code to follow our WeChat official account
