云数据库 GAUSSDB-循环语句:FOR_LOOP(integer变量)语句

时间:2024-11-13 14:45:53

FOR_LOOP(integer变量)语句

语法图

图3 for_loop::=

label declaration ::=

  • 变量name会自动定义为integer类型并且只在此循环里存在。变量name介于lower_bound和upper_bound之间。
  • 当使用REVERSE关键字时,lower_bound必须大于等于upper_bound,否则循环体不会被执行。

示例

 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
--从0到5进行循环
gaussdb=# CREATE OR REPLACE PROCEDURE proc_for_loop()
AS
    BEGIN
    FOR I IN 0..5 LOOP
        DBE_OUTPUT.PRINT_LINE('It is '||to_char(I) || ' time;') ;
    END LOOP;
END;
/
CREATE PROCEDURE

--调用存储过程
gaussdb=# CALL proc_for_loop();
It is 0 time;
It is 1 time;
It is 2 time;
It is 3 time;
It is 4 time;
It is 5 time;
 proc_for_loop 
---------------

(1 row)

--删除存储过程
gaussdb=# DROP PROCEDURE proc_for_loop;
DROP PROCEDURE
support.huaweicloud.com/centralized-devg-v8-gaussdb/gaussdb-42-0788.html