§ Oracle兼容-存储过程-FOR LOOP


§ 1. 语法

FOR var IN [REVERSE] expr1 .. expr2 LOOP .. END LOOP

-- OR
FOR var IN [REVERSE] expr1..expr2 LOOP .. END LOOP
1
2
3
4

§ 2. 定义和用法

在 GreatSQL 中支持用 FOR .. IN expr1 .. expr2 LOOP .. END LOOP 语法循环读取数据,并赋值给相应变量。在 FOR ... LOOP 中,支持前后两个参数表达式和中间的点号连接在一起,例如:FOR .. IN expr1..expr2 LOOP;或者只和一个参数连接,例如:FOR .. IN expr1.. expr2 LOOPFOR .. IN expr1 ..expr2 LOOP 都是可以的。

§ 3. Oracle兼容说明

ORACLE 模式下,GreatSQL存储过程支持 FOR .. LOOP 用法。该用法如下所述:

  1. 不需要事先声明 FOR LOOP 中的变量 var,直接使用 FOR var IN expr1 .. expr2 LOOP 语法即可。

  2. 支持增加 REVERSE 关键字,可以实现倒序获取数据目的。

  3. 循环中的 expr1/expr2 支持数值类型变量、数值、函数以及表达式等。expr1/expr2 如果是 FLOAT 类型则会被转换成 INT 型(转换时会做四舍五入处理)。

  4. 如果 expr1/expr2 是时间类型,则会被转换为数值类型进行计算,而不是按照时间规则进行加减计算。在Oracle中不支持该类型,会产生报错。

  5. 在循环 FOR var IN 中的变量 var 如果是 FLOAT 类型则会被转换成 INT 型(转换时会做四舍五入处理)。

§ 4. 示例

    1. 示例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

可以看到,当传入参数是 FLOAT 类型是,会被转换成 INT 型(转换时会做四舍五入处理)。

    1. 示例2
greatsql> SET sql_mode = ORACLE;
greatsql> DELIMITER //

greatsql> CREATE OR REPLACE PROCEDURE p1(i FLOAT, j FLOAT) AS
BEGIN
  -- 倒序
  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. 示例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
    -- 循环内部再加上一个 FLOAT 类型值
    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

扫码关注微信公众号

greatsql-wx