← PL/SQL Topics

Language Basics

Explain nested PL/SQL blocks and block labels

Nested Blocks and Labels

Oracle Fusion / EBS · Technical · PL/SQL

GeneralHigh confidence

Blocks can nest inside each other, and labels let you target a specific outer block or loop by name.

Grounded in: Curated Oracle knowledge layer — PL/SQL

How it works

PL/SQL blocks can nest — an inner BEGIN...END is a full block with its own DECLARE section, and it can read variables from any enclosing block (an inner variable with the same name shadows the outer one). A label (<<label_name>>) placed before a block or loop lets a later EXIT, CONTINUE, GOTO, or a qualified reference (label.variable) target that specific block or loop by name, which matters once loops are nested.

How it works
<<outer_block>>
DECLARE
  v_total NUMBER := 0;
BEGIN
  <<outer_loop>>
  FOR v_i IN 1..3 LOOP
    <<inner_loop>>
    FOR v_j IN 1..3 LOOP
      IF v_j = 2 THEN
        CONTINUE outer_loop;
      END IF;
      outer_block.v_total := outer_block.v_total + 1;
    END LOOP inner_loop;
  END LOOP outer_loop;
  DBMS_OUTPUT.PUT_LINE('Total: ' || v_total);
END outer_block;
/

Related Questions

Have a follow-up, or a different question?

Continue in Ask Oracle AI