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.




