Updated on 2024-06-03 GMT+08:00

Loop Statements

Simple LOOP Statements

Syntax diagram

Figure 1 loop::=

label declaration ::=

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
gaussdb=# CREATE OR REPLACE PROCEDURE proc_loop(i in integer, count out integer) 
AS 
    BEGIN 
        count:=0; 
        LOOP 
        IF count > i THEN 
            raise info 'count is %. ', count;  
            EXIT; 
        ELSE 
            count:=count+1; 
        END IF; 
        END LOOP; 
    END;
/
CREATE PROCEDURE

gaussdb=# CALL proc_loop(10,5);
INFO:  count is 11. 
 count 
-------
    11
(1 row)

The loop must be exploited together with EXIT; otherwise, a dead loop occurs.

WHILE_LOOP Statements

Syntax diagram

Figure 2 while_loop::=

label declaration ::=

If the conditional expression is true, a series of statements in the WHILE statement are repeatedly executed and the condition is decided each time the loop body is executed.

Example

 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
gaussdb=# CREATE TABLE integertable(c1 integer) ; 
CREATE TABLE
gaussdb=# CREATE OR REPLACE PROCEDURE proc_while_loop(maxval in integer) 
AS 
    DECLARE 
    i int :=1;  
    BEGIN 
        WHILE i < maxval LOOP 
            INSERT INTO integertable VALUES(i); 
            i:=i+1; 
        END LOOP; 
    END; 
/
CREATE PROCEDURE

-- Call the stored procedure.
gaussdb=# CALL proc_while_loop(10);
 proc_while_loop 
-----------------

(1 row)

-- Delete the stored procedure and table.
gaussdb=# DROP PROCEDURE proc_while_loop;
DROP PROCEDURE
gaussdb=# DROP TABLE integertable;
DROP TABLE

FOR_LOOP (Integer variable) Statement

Syntax diagram

Figure 3 for_loop::=

label declaration ::=

  • The variable name is automatically defined as the integer type and exists only in this loop. The variable name falls between lower_bound and upper_bound.
  • When the keyword REVERSE is used, the lower bound must be greater than or equal to the upper bound; otherwise, the loop body is not executed.

Example

 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
-- Loop from 0 to 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

-- Call the stored 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)

-- Delete the stored procedure.
gaussdb=# DROP PROCEDURE proc_for_loop;
DROP PROCEDURE

FOR_LOOP Query Statements

Syntax diagram

Figure 4 for_loop_query::=

label declaration ::=

The variable target is automatically defined, its type is the same as that in the query result, and it is valid only in this loop. The target value is the query result.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-- Display the query result from the loop.
gaussdb=# CREATE OR REPLACE PROCEDURE proc_for_loop_query()
AS 
    record VARCHAR2(50);
BEGIN 
    FOR record IN SELECT spcname FROM pg_tablespace LOOP 
    dbe_output.print_line(record); 
    END LOOP; 
END; 
/
CREATE PROCEDURE

-- Call the stored procedure.
gaussdb=# CALL proc_for_loop_query();
pg_default
pg_global
 proc_for_loop_query 
---------------------

(1 row)

-- Delete the stored procedure.
gaussdb=# DROP PROCEDURE proc_for_loop_query;
DROP PROCEDURE

FORALL Batch Query Statements

Syntax diagram

Figure 5 forall::=

label declaration ::=

  • The variable index is automatically defined as the integer type and exists only in this loop. The index value falls between low_bound and upper_bound.
  • If SAVE EXCEPTIONS is specified, exceptions occurred during DML execution in the loop body are saved in SQL&BULK_EXCEPTIONS and an exception is thrown after the execution is complete. If there is no abnormal execution result in the loop, the loop will not be rolled back in the current subtransaction.

Example

 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
gaussdb=# CREATE TABLE hdfs_t1 (
  title NUMBER(6),
  did VARCHAR2(20),
  data_period VARCHAR2(25),
  kind VARCHAR2(25),
  interval VARCHAR2(20),
  time DATE,
  isModified VARCHAR2(10)
);
CREATE TABLE

gaussdb=# INSERT INTO hdfs_t1 VALUES( 8, 'Donald', 'OConnell', 'DOCONNEL', '650.507.9833', to_date('21-06-1999', 'dd-mm-yyyy'), 'SH_CLERK' );
INSERT 0 1

gaussdb=# CREATE OR REPLACE PROCEDURE proc_forall()
AS 
BEGIN 
    FORALL i IN 100..120 
        update hdfs_t1 set title = title + 100*i;
END; 
/
CREATE PROCEDURE

-- Call the stored procedure.
gaussdb=# CALL proc_forall();
 proc_forall 
-------------

(1 row)

-- Query the invocation result of the stored procedure.
gaussdb=# SELECT * FROM hdfs_t1;
 title  |  did   | data_period |   kind   |   interval   |        time         | ismodified 
--------+--------+-------------+----------+--------------+---------------------+------------
 231008 | Donald | OConnell    | DOCONNEL | 650.507.9833 | 1999-06-21 00:00:00 | SH_CLERK
(1 row)

-- Delete the stored procedure and table.
gaussdb=# DROP PROCEDURE proc_forall;
DROP PROCEDURE

gaussdb=# DROP TABLE hdfs_t1;
DROP TABLE