Statement labels for nested compound statements in native SQL procedures
You can define a label for each compound statement in an SQL procedure. This label enables you to reference this block of statements in other statements such as the GOTO, LEAVE, and ITERATE SQL PL control statements. You can also use the label to qualify a variable when necessary. Labels are not required.
A label name must meet the following criteria:
You can reference a label within the compound statement in which it is defined, including any compound statements that are nested within that compound statement.
Example of statement labels: The following example shows
several statement labels and their scope:
CREATE PROCEDURE P1 ()
LANGUAGE SQL
--Outermost compound statement ------------------------
OUTER1: BEGIN 1
--Inner compound statement with label INNER1 ---
INNER1: BEGIN 2
IF...
ABC: LEAVE INNER1; 3
ELSEIF
XYZ: LEAVE OUTER1; 4
END IF
END INNER1;
--End of inner compound statement INNER1 ------
--Inner compound statement with label INNER2---
INNER2: BEGIN 5
XYZ:...statement 6
END INNER2;
-- End of inner compound statement INNER2 -----
END OUTER1 7
The preceding
example has the following parts: The following examples show valid and invalid uses of labels:
Invalid example of labels:
L1: BEGIN
L2: SET A = B;
L1: GOTO L2: --This duplicate use of the label L1 causes an error, because
--the same label is already used in the same scope.
END L1;
Valid example of labels:
L1: BEGIN
L2: BEGIN
L4: BEGIN --This line contains the first use of the label L4
DECLARE A CHAR(5);
SET A = B;
END L4;
END L2;
L3: BEGIN
L4: BEGIN --This second use of the label L4 is valid, because
--it is used in a different scope.
DECLARE A CHAR(5);
SET A = B;
END L4;
END L3;
END L1;