§ Oracle Compatibility - Stored Procedure - GOTO
§ 1. Syntax
GOTO label
The statement block after label supports the following usages:
{
sp_proc_stmt_statement
| exit [label]/exit [label] when
| leave label
| iterate label
| goto label
| open cursor
| fetch cursor
| close cursor
| return
| if elsif
| case when
| forall loop
| declare body begin .. end
| sp_labeled_control
}
sp_proc_stmt_statement:
{
DDL_statement ...
DML_statement ...
SHOW_statement ...
}
sp_labeled_control:
{
for_loop_stmt
| while loop
| loop end loop
| repeat until
}
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
§ 2. Definition and Usage
GreatSQL stored procedures support using label to mark a position and GOTO label to jump to the specified marked position.
Note: use the GOTO syntax with caution, as it can easily cause infinite loops.
§ 3. Oracle Compatibility Notes
GreatSQL stored procedures support using label to mark a position and GOTO label to jump to the specified marked position. The usage is as follows:
If the
GOTOstatement exits a cursorFOR LOOPstatement block, the cursor is closed.GOTOcannot be used to transfer control into statements or sub-blocks such asIF / CASE / LOOP.GOTOcannot be used to transfer control from oneIFstatement clause to another, or from oneCASE WHENclause to another.GOTOcannot be used to transfer control out of a subprogram.GOTOcannot be used to transfer control into an exception handler.GOTOcannot be used to transfer control from an exception handler back to the current block (but it can transfer control from an exception handler to an enclosing block).GOTOcan jump to anywhere except the cases mentioned above. GreatSQL's nativeLEAVE labelcan only be used withinLOOPandBEGIN ENDblocks.Only a single label marker is supported; multiple labels marking the same position are not supported. See the example below:
<<label1>>
<<label12>> -- Marking two positions at the same time is not supported; one of them must be removed
a := b;
2
3
- The
GOTOsyntax is supported inEVENT / TRIGGER / PACKAGE / anonymous blocks. If a logic error inEVENT / TRIGGERmay cause an infinite loop, it wastes system resources, so it should be used with caution.
§ 4. Examples
- Example 1: jump forward
greatsql> SET sql_mode = ORACLE;
greatsql> DELIMITER //
greatsql> CREATE OR REPLACE PROCEDURE goto_sp1(v_str IN OUT VARCHAR)
AS
BEGIN
v_str := 'label1';
GOTO label1;
<<label1>>
-- Jump to label2 immediately, i.e., the RETURN completes
GOTO label2;
v_str := 'label2';
<<label2>>
RETURN;
END; //
greatsql> CALL goto_sp1(@v_str) //
Query OK, 0 rows affected (0.00 sec)
greatsql> SELECT @v_str //
+--------+
| @v_str |
+--------+
| label1 |
+--------+
1 row in set (0.00 sec)
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
- Example 2: jump backward
greatsql> SET sql_mode = ORACLE;
greatsql> DELIMITER //
greatsql> CREATE OR REPLACE PROCEDURE goto_sp2(v_str IN OUT VARCHAR)
AS
BEGIN
v_str := 'label1';
<<label1>>
IF (v_str = 'label2') THEN
RETURN;
END IF;
v_str := 'label2';
GOTO label1;
END; //
greatsql> CALL goto_sp2(@v_str) //
greatsql> SELECT @v_str //
+--------+
| @v_str |
+--------+
| label2 |
+--------+
1 row in set (0.00 sec)
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
- Example 3: conditional handling is supported
greatsql> SET sql_mode = ORACLE;
greatsql> DELIMITER //
greatsql> CREATE OR REPLACE PROCEDURE goto_sp3(n INT, ret OUT VARCHAR)
AS
a INT;
BEGIN
<<label1>>
BEGIN
SELECT a INTO a FROM information_schema.TABLES LIMIT n;
EXCEPTION
WHEN TOO_MANY_ROWS THEN
BEGIN
ret := ret||'--- too_many_rows cought 1 ---';
GOTO label2;
END;
WHEN NO_DATA_FOUND THEN
BEGIN
ret := ret||'--- no_data_found cought 1 ---';
n := 2;
SELECT a INTO a FROM information_schema.TABLES LIMIT n;
EXCEPTION
WHEN TOO_MANY_ROWS THEN
BEGIN
ret := ret||'--- too_many_rows cought 2 ---';
GOTO label1;
END;
WHEN NO_DATA_FOUND THEN ret := '--- no_data_found cought 2 ---';
END;
END;
SET ret := ret||' error ';
<<label2>>
RETURN;
END; //
greatsql> CALL goto_sp3(0, @ret) //
Query OK, 0 rows affected (0.00 sec)
greatsql> SELECT @ret //
+--------------------------------------------------------------------------------------------+
| @ret |
+--------------------------------------------------------------------------------------------+
| --- no_data_found cought 1 ------ too_many_rows cought 2 ------ too_many_rows cought 1 --- |
+--------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
greatsql> CALL goto_sp3(0, @ret) //
Query OK, 0 rows affected (0.00 sec)
greatsql> SELECT @ret //
+--------------------------------------------------------------------------------------------+
| @ret |
+--------------------------------------------------------------------------------------------+
| --- no_data_found cought 1 ------ too_many_rows cought 2 ------ too_many_rows cought 1 --- |
+--------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
greatsql> CALL goto_sp3(3, @ret) //
Query OK, 0 rows affected (0.00 sec)
greatsql> SELECT @ret //
+--------------------------------+
| @ret |
+--------------------------------+
| --- too_many_rows cought 1 --- |
+--------------------------------+
1 row in set (0.00 sec)
greatsql> CALL goto_sp3(1, @ret) //
Query OK, 1 row affected (0.00 sec)
greatsql> SELECT @ret //
+---------+
| @ret |
+---------+
| error |
+---------+
1 row in set (0.00 sec)
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
78
79
- Example 4: cannot jump into a
CASE WHENstatement block
- Example 4: cannot jump into a
greatsql> SET sql_mode = ORACLE;
greatsql> DELIMITER //
greatsql> CREATE OR REPLACE PROCEDURE goto_sp4()
AS
a INT;
BEGIN
GOTO label1;
case 1
WHEN 1 THEN
<<label1>>
a := 2;
WHEN 2 THEN
a := 1;
END CASE;
RETURN;
END; //
ERROR 1308 (42000): GOTO with no matching label: label1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- Example 5: cannot jump into another
CASE WHENstatement block
- Example 5: cannot jump into another
greatsql> SET sql_mode = ORACLE;
greatsql> DELIMITER //
greatsql> CREATE OR REPLACE PROCEDURE goto_sp5()
AS
a INT;
BEGIN
CASE 1
WHEN 1 THEN
<<label1>>
a := 2;
WHEN 2 THEN
a := 1;
GOTO label1;
end case;
RETURN;
END; //
ERROR 1308 (42000): GOTO with no matching label: label1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Scan the QR code to follow our WeChat official account
