GaussDB SQL basic syntax example-loop statement

Table of contents

I. Introduction

2. General loop statements of GaussDB database

3. Commonly used loop statements in GaussDB (grammar + examples)

1. LOOP loop statement

2. WHILE ... LOOP loop statement

3. FOR...LOOP loop statement (integer variable)

4. FORALL loop statement (batch query)

4. Summary

I. Introduction

SQL is a standard computer language used to access and manipulate databases. GaussDB supports the SQL standard (main features of SQL2, SQL3 and SQL4 are supported by default).

This series will be introduced based on the "Cloud Database GaussDB—SQL Reference".

2. General loop statements of GaussDB database

In the GaussDB database, the loop statement is a commonly used control flow statement used to perform repeated operations in database queries. By using loop statements, we have the flexibility to process each row in the data set and transform, filter, or calculate the data as needed. Loop statements have significant advantages when processing large-scale data and performing complex calculations and operations.

In the GaussDB database, loop statements mainly include statements such as LOOP, WHILE LOOP, FOR LOOP and FORALL. These statements are used to perform repeated operations in a database query, and you can choose the appropriate loop type as needed to process each row in the data set. This article will introduce loop statements and their applications in the GaussDB database.

3. Commonly used loop statements in GaussDB (grammar + examples)

1, LOOP circular phrase

A LOOP loop is an infinite loop until explicitly exited using the EXIT statement. Within the loop body, repeated query operations can be performed based on conditions. For example, you can use an IF statement to check a condition each time through the loop and perform the appropriate action.

grammar:

LOOP

statements 

END LOOP;

Tip: This loop must be used in conjunction with EXIT, otherwise it will fall into an infinite loop.

Example:

---Insert multiple pieces of data, i is the starting value of field id, and j is the ending value.

CREATE OR REPLACE PROCEDURE proc_loop(i in integer,j in integer)

AS

  BEGIN 

     LOOP 

         ​​​​—Perform repeated interpolation operations 

         INSERT INTO test_1(id,c_times) VALUES(i,current_timestamp) ;

                 

                 ​​​​​​—ID value increases by +1

                 i := i+1;             

     

         ​​​​—Conditions for exiting the loop 

         IF i=j THEN 

            EXIT; 

         END IF; 

      END LOOP; 

   END;

 /

CALL proc_loop(1,5);

SELECT * FROM test_1;

2, WHILE … LOOP circular phrase

The WHILE ... LOOP loop is a conditional loop that will continue to execute as long as the specified conditions are met. Within the loop body, repeated query operations can be performed. The loop terminates when the condition is no longer met.

grammar:

WHILE condition LOOP

statements

END LOOP;

Example:

---Insert multiple pieces of data, i is the starting value of field id, and j is the ending value.

CREATE OR REPLACE PROCEDURE proc_loop_1(i in integer,j in integer)

AS

BEGIN 

  WHILE i < j LOOP 

    --Perform repeated query operations 

    INSERT INTO test_1(id,c_times) VALUES(i,current_timestamp);

       

        ​​​​—Id value increases by +1

      i := i+1;

  END LOOP; 

END;

/

CALL proc_loop_1(5,10);

SELECT * FROM test_1;

3, FOR ... LOOP loop statement (integer variable)

The FOR LOOP loop executes a loop a specified number of times. Within the loop body, repeated query operations can be performed.

grammar:

FOR name IN [REVERSE] lower_bound ..upper_bound [BY step] LOOP

staements

END LOOP;

Tip: The variable name is automatically defined as integer type and only exists in this loop. The variable name is between lower_bound and upper_bound. When using the REVERSE keyword, lower_bound must be greater than or equal to upper_bound, otherwise the loop body will not be executed.

Example:

---Insert multiple pieces of data, i is the starting value of field id, and j is the ending value.

CREATE OR REPLACE PROCEDURE proc_loop_2(i in integer,j in integer)

AS

BEGIN 

  FOR i IN i..j LOOP 

    --Perform repeated query operations 

    INSERT INTO test_1(id,c_times) VALUES(i,current_timestamp);

       

  END LOOP; 

END;

/

CALL proc_loop_2(10,15);

SELECT * FROM test_1;

4, FORALL loop statement (batch query)

The FORALL loop is a parallelized loop used to perform parallel operations on a data set. It is suitable for processing large-scale data sets and can utilize multi-core processors to execute query operations in parallel.

grammar:

FORALL index IN lower_bound .. upper_bound DML;

Tip:The variable index is automatically defined as integer type and only exists in this loop. The value of index is between low_bound and upper_bound.

Example:

---Insert multiple pieces of data,

CREATE OR REPLACE PROCEDURE proc_loop_3(i in integer,j in integer)

AS

BEGIN

    FORALL i IN i..j

       ​​​—Perform repeated query operations 

       INSERT INTO test_1(id,c_times) VALUES(i,current_timestamp);

END;

/

CALL proc_loop_3(16,20);

SELECT * FROM test_1;

4. Summary

This article introduces loop statements in the GaussDB database and how to use them. Loop statements provide flexible data processing capabilities. By mastering the usage of loop statements, we can better utilize the functions of GaussDB database and improve data processing efficiency and query performance.

--Finish

Guess you like

Origin blog.csdn.net/GaussDB/article/details/134370295