본문 바로가기

Note

[SQL] 익명 블록 (Anonymous Block)

V9.7에서 소개된 개념으로 FixPack5에서 약간 기능(locking 처리)  개선되어진 것으로 알고 있다.

이 기능이 업무 단에 실질적으로 적용될 가능성은 없을 것 같지만, 개발자 측면에서는 편의성을 제공해 주는 기능이 아닐까 싶다.

routine 을 프로그래밍 하면서, 함수나 저장 프로시저(stored procedure)의 로직 검증을 하기 위해 매번 삭제하고 컴파일하여 db에 반영(deploy)하는 작업을 거쳐야 한다. 어떤 면에서는 불필요하게 반영(deploy)하는 것을 꼭 해야하나? 하는 의문이 들 것이다.

특히 운영 시스템에 routine들을 적용해야 하는 시점에서 검증을 하기 위해 컴파일해서 반영(deploy)하는 것도 때로는 부담이 되고 번거롭게 하는 과정일 수도 있을 것이다.

이렇게 db에 반영하지 않고 로직 compile하여 실행시키는 일련의 작업만 하도록 제공하는 것이 “익명 블록”이다. PLSQL이나 SQLPL 작업 시 로직 검증은 편하게 할 수 있는 방법이 되지 않을까 싶다.

이 테스트는 DB2_COMPATIBILITY_VECTOR=ORA 적용하고 수행되었다.

익명 블록 코드 예 - 3번 반복하면서 시간소 및 반복 회수를 도출하는 로직

db2 +p -td@ -v <<EOF
connect to sample @
set serveroutput on @

declare
    ret_val integer ;
    ret_time timestamp;
   
begin
      for k in 1..3 loop
          select k, current timestamp into ret_val, ret_time from dual;
          dbms_output.put_line('count number: ' || ret_val);
          dbms_output.put_line('loop time: ' || ret_time) ;
      end loop ;
end;
@
set serveroutput off @
terminate @
EOF

 

실행 결과

블록을 컴파일하면 컴파일과 동시에 실행이 되어 진다. 로직에 대한 논리적인 이름(껍데기?)을 갖고 있지 못하기에 당연한 결과일 수 있을 것이다.

connect to sample
 
   Database Connection Information
 
Database server        = DB2/LINUXX8664 9.7.4
SQL authorization ID   = ORA974
Local database alias   = SAMPLE
 
 
set serveroutput on
DB20000I  The SET SERVEROUTPUT command completed successfully.
 
declare ret_val integer ; ret_time timestamp; begin for k in 1..3 loop select k, current timestamp into ret_val, ret_time from dual; dbms_output.put_line('count number: ' || ret_val); dbms_output.put_line('loop time: ' || ret_time) ; end loop ; end;
DB20000I  The SQL command completed successfully.
 
count number: 1
loop time: 2011-10-31-00.03.21.099553
count number: 2
loop time: 2011-10-31-00.03.21.124113
count number: 3
loop time: 2011-10-31-00.03.21.124266
 
set serveroutput off
DB20000I  The SET SERVEROUTPUT command completed successfully.
 
terminate
DB20000I  The TERMINATE command completed successfully.