§ 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
1
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:

  1. The variable var in FOR LOOP does not need to be declared in advance; the FOR var IN expr1 .. expr2 LOOP syntax can be used directly.

  2. The REVERSE keyword can be added to obtain data in reverse order.

  3. expr1/expr2 in the loop support numeric-type variables, numbers, functions, expressions, etc. If expr1/expr2 is of FLOAT type, it is converted to INT type (rounded during conversion).

  4. If expr1/expr2 is 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.

  5. In the loop FOR var IN, if the variable var is of FLOAT type, it is converted to INT type (rounded during conversion).

§ 4. Examples

    1. 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)
1
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).

    1. 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)
1
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
    1. 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)
1
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

greatsql-wx