The BEGIN ... END statement gives the statements defined within the BEGIN and END keywords the status of a single statement.
The second Label can be present only if the first Label is present. If both labels are present, they must be identical. Two or more labeled statements at the same level can have the same label, but this partly negates the advantage of the second label. The advantage is that the labels unambiguously and accurately match each END with its BEGIN. However, a labeled statement nested within Statements cannot have the same label, because this makes the behavior of the ITERATE and LEAVE statements ambiguous.
DECLARE Variable1 CHAR 'Existing variable'; -- A reference to Variable1 here returns 'Existing variable' BEGIN -- A reference to Variable1 here returns 'Existing variable' DECLARE Variable1 CHAR 'Local variable'; -- Perfectly legal even though the name is the same -- A reference to Variable1 here returns 'Local variable' END;
You can consider the BEGIN ... END statement to be a looping construct, which always loops just once. The effect of an ITERATE or LEAVE statement nested within a BEGIN ... END statement is then as you would expect: control is transferred to the statement following the END. Using ITERATE or LEAVE within a BEGIN ... END statement is useful in cases where there is a long series of computations that needs to be abandoned, either because a definite result has been achieved or because an error has occurred.