GaussDB SQL basic syntax example-GOTO statement

Table of contents

I. Introduction

2. Concepts and syntax in GaussDB database

1. Basic concepts

2. Grammar

3. Basic examples and restricted scenario descriptions in GaussDB database

1. Basic example

2. Description of restricted scenarios

fourth、小结

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 online document "Cloud Database GaussDB—SQL Reference".

The GOTO statement is an intuitive and basic control flow statement that causes unconditional changes in control flow. It is used to offload to a specific user-defined location using a label defined in the SQL procedure.

Using the GOTO statement is generally considered a lack of programming skill and is not recommended. Extensive use of GOTO can lead to poor readability of code, especially when the process becomes long. Additionally, GOTO is not necessary because there are better statements available for controlling the execution path. There are no specific circumstances that require the use of GOTO; it is generally used just for convenience.

2. Concepts and syntax inGaussDB database

1, Basic Concepts

The GOTO statement is a control statement that is used to unconditionally jump the execution of the program to a specified location. In the SQL implementation of GaussDB database, GOTO statements are usually used in database objects such as stored procedures and triggers to implement complex logic control.

The GOTO statement can realize an unconditional jump from the GOTO position to the target statement. The GOTO statement will change the original execution logic, so it should be used with caution. When executing a GOTO statement, the target Label must be unique.

2、Language

BEGIN
     --some code here
     IF condition THEN
        GOTO label;
     END IF;
     
     -- some code here  
   GOTO label;
		
<<label>>
-- code to jump to

END;
/

3. Basic examples and restricted scenario descriptions inGaussDB database

1, basic example

Create a function that jumps through the GOTO statement when certain conditions are met.

CREATE OR REPLACE FUNCTION  proc_goto(i in integer,j in integer)
RETURNS TEXT
LANGUAGE plpgsql
AS $$
  BEGIN  
     LOOP
         INSERT INTO test_1(id,date) VALUES(i,current_date) ;
	     i := i+1;      
         IF i=j THEN  
		  	 GOTO label;					 
         END IF;  
     END LOOP;
			
     <<label>>			
   INSERT INTO test_1(id,date) VALUES(i,'3000-12-31') ;
			
	 RETURN 'succeed';
  END $$;
 
CALL proc_goto(1,5);

select * from test_1;

2, limited view description

GOTOUsage has the following restricted scenarios:

1. Multiple target scenarios with the same GOTO labels are not supported, regardless of whether they are in the same block.

BEGIN
  GOTO label 1; 
  << label 1>>
  SELECT * FROM ...
  << label 1>>
  UPDATE t1 SET ...
END;

2. GOTO jumps to IF statements, CASE statements, and LOOP statements are not supported.

BEGIN
   GOTO label 1; 
   IF valid THEN
     << label 1>>
     SELECT * FROM ...
   END IF;
 END;

3. The GOTO statement does not support jumping from one IF clause to another IF clause, or jumping from the WHEN clause of a CASE statement to another WHEN clause.

BEGIN 
   IF valid THEN
     GOTO label 1;
     SELECT * FROM ...
   ELSE
     << label 1>>
     UPDATE t1 SET ...
   END IF;
 END;

4. Jumping from the external block to the internal BEGIN-END block is not supported, that is, jumping from the outer layer to the inner layer is prohibited.

BEGIN
   GOTO label 1;  
   BEGIN
     << label 1>>
     UPDATE t1 SET ...
   END;
 END;

5. Jumping from the exception handling part to the current BEGIN-END block is not supported. But you can jump to the upper BEGIN-END block.

BEGIN
   << label 1>>
   UPDATE t1 SET ...
   EXCEPTION
     WHEN condition THEN
        GOTO label 1;
 END;

6. If you go from GOTO to a location that does not contain an execution statement, you need to add a NULL statement.

DECLARE
   done  BOOLEAN;
BEGIN
   FOR i IN 1..50 LOOP
      IF done THEN
         GOTO end_loop;
      END IF;
      <<end_loop>>  
      NULL; 
   END LOOP;  
END;
/

4. Summary

In this article, we introduce the usage characteristics of GOTO statements in cloud database GaussDB SQL. Through a series of examples, we can better organize the code and avoid unnecessary calculations and operations, thereby improving the execution efficiency of SQL statements. But at the same time, it should be noted that although the GOTO statement can be used to implement complex logic control, it can also easily lead to reduced readability of the code and difficulty in maintenance. Therefore, in actual development, the GOTO statement should be used with caution according to specific circumstances.

--Finish

Guess you like

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