PL/SQL 变量范围和内部外部块:嵌套结构
⚡ 智能摘要
PL/SQL 中的嵌套代码块可以将一个代码块放置在另一个代码块内部,从而分离复杂的逻辑并隔离异常处理。了解外部代码块和内部代码块之间的变量作用域,并使用代码块标签,可以避免同名变量产生歧义。

什么是嵌套块 Oracle?
In PL / SQL每个代码块都可以嵌套在另一个代码块中。这样的代码块被称为嵌套代码块。当需要在保持代码块完整性的同时执行特定操作时,嵌套代码块非常常见。ping 该过程的代码位于单独的容器(代码块)中。
嵌套块的概念通过将复杂的逻辑拆分成单独的块,帮助程序员提高代码的可读性,并且它允许…… 例外 每个块都要在主外部块内进行处理。
嵌套块结构
一个代码块可以嵌套在另一个代码块中。它可以嵌套在执行部分或异常处理部分,并且这些代码块也可以被标记。一个外部代码块可以包含多个内部代码块。每个内部代码块本身也是一个 PL/SQL 块因此,内部代码块的所有属性和特征都与外部代码块相同。父代码块是主代码块,子代码块是嵌套代码块。
下图以图形方式表示了嵌套块结构,其中父块是主块,子块是嵌套块。
以下是嵌套代码块的语法。下面的屏幕截图显示了嵌套代码块的语法,并标注了外部代码块和内部代码块。
嵌套块语法
<<outer_block>> DECLARE <Declarative section> BEGIN <Execution part for outer block begins> <<inner block>> DECLARE <Declarative section> BEGIN <Execution part for inner block>. EXCEPTION <Exception handling part> END; <Execution part for outer block begins> EXCEPTION <Exception handling part> END;
- 上面的语法显示了一个嵌套块,其中总共包含两个块。
- These blocks are labelled as 'outer_block' and 'inner_block'.
嵌套块中的作用域:变量作用域
在嵌套代码块中,使用每个代码块之前,需要清楚地了解其作用域和可见性。尤其是在内层代码块中,内外层代码块的元素都是可见的,因此必须对此有透彻的理解。
以下几点总结了嵌套块中的作用域。
- 外部代码块中声明的元素,以及在内部代码块定义之前定义的值,在内部代码块中是可见的。
- 内部代码块中声明的元素在外部代码块中不可见,它们仅在内部代码块内可见。
- 外块和内块可以有 变量 同名。
- 对于同名变量,内部代码块默认只引用内部代码块中声明的变量。
- If the inner block wants to refer to the outer block variable that has the same name as the inner block variable, then the outer block must be LABELLED, and the outer block variable can be referred to as '<outer_block_label>.<variable_name>'.
下面的例子可以帮助你更好地理解这些作用域。
例如1: 在这个例子中,我们可以看到内部代码块和外部代码块中变量的作用域。我们还可以看到如何使用代码块标签来引用这些变量。
下面截图显示了示例 1,其中内部块使用块标签引用外部块变量。
<<OUTER_BLOCK>> DECLARE var1 VARCHAR2(30):='outer_block'; var2 VARCHAR2(30):='value before inner block'; BEGIN <<INNER_BLOCK>> DECLARE var1 VARCHAR2(30):='inner_block'; BEGIN dbms_output.put_line(var1); dbms_output.put_line(OUTER_BLOCK.var1); dbms_output.put_line(var2); END; var2:='value after inner block'; END; /
Code 说明:
- Code 第1行: Labelling the outer block as "OUTER_BLOCK".
- Code 第3行: Declaring a variable 'var1' as VARCHAR2 (30) with the initial value of "outer_block".
- Code 第4行: Declaring a variable 'var2' as VARCHAR2 (30) with the initial value of "value before inner block".
- Code 第6行: Labelling the inner block as "INNER_BLOCK".
- Code 第8行: Declaring a variable 'var1' in the inner block as VARCHAR2 (30) with the initial value of "inner_block".
- Code 第10行: Printing the value of 'var1'. Since no label is mentioned, by default it takes the value from the inner block, hence printing the 'inner_block' message.
- Code 第11行: Printing the value of the outer block variable 'var1'. Since the inner block has a variable with the same name, we refer to the outer block label. Thus it prints the 'outer_block' message.
- Code 第12行: Printing the value of the outer block variable 'var2'. Since there is no variable with this name in the inner block, by default it takes the value from the outer block, hence printing the 'value before inner block' message.
- The variable 'var2' in the outer block is assigned the value 'value after inner block'. But this assignment happens after the definition of the inner block, so this value is not present inside the inner block.
例如2: 在这个例子中,我们计算两个数字之间的差值,一个数字声明在外层代码块中,另一个数字声明在内层代码块中。这两个数字具有相同的名称。让我们看看代码块标签是如何用于引用这些变量的。
下面截图显示了示例 2,它计算同名外部块变量和内部块变量之间的差异。
<<OUTER_BLOCK>> DECLARE ln_val NUMBER :=5; BEGIN <<INNER_BLOCK>> DECLARE ln_val NUMBER :=3; BEGIN dbms_output.put_line('The difference between outer block and inner block variable is:'||outer_block.ln_val-inner_block.ln_val); END; END; /
Code 说明:
- Code 第1行: Labelling the outer block as "OUTER_BLOCK".
- Code 第3行: Declaring a variable 'ln_val' as NUMBER with the initial value of "5".
- Code 第5行: Labelling the inner block as "INNER_BLOCK".
- Code 第7行: Declaring a variable 'ln_val' in the inner block as NUMBER with the initial value of "3".
- Code 第9行: Printing the difference in value of 'ln_val' from the outer and inner block. The "<block_name>.<variable_name>" format is used to refer to these variables to avoid conflicts due to the same variable name.




