본문 바로가기

프로그램/oracle

[ORACLE]RETURNING - plsql에서 dml 실행한 항목의 값을 return 받고 싶을 경우 사용

반응형

RETURNING - plsql에서 dml 실행한 항목의 값을 return 받고 싶을 경우 사용



plsql에서 dml 실행한 항목의 값을 return 받고 싶을 경우 사용

- update 문의 경우 update된 값을 반환

- delete 는 삭제된 값 반환


insert select 문에서는 사용할 수 없음


시퀀스를 insert 구문에 바로 사용한 것을 받아올 경우에 유용하게 사용할 수 있을 것 같음


* 참고 URL

https://oracle-base.com/articles/misc/dml-returning-into-clause

https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/returninginto_clause.htm



* 샘플


set serveroutput on  


declare

    lv_rtn_lot varchar2(100);

    

    type tbl_rtn is table of varchar2(100);

    lt_rtn tbl_rtn;

    

    type tbl_emi is table of T_TEST_TBL%rowtype;

    lt_emi tbl_emi;

    

    type rec_emi is record(

        wcode T_TEST_TBL.wcode%type

        , lot T_TEST_TBL.lot%type

        , pono T_TEST_TBL.pono%type

    );

    

    type tbl_emi2 is table of rec_emi;

    lt_emi2 tbl_emi2;

    

begin

    --1건 처리 시

    insert into T_TEST_TBL(lot)

--    select 'XXX' from dual /*select 로 insert시에는 사용불가*/

    values('xxx')

    returning lot into lv_rtn_lot

    ;

    

    dbms_output.put_line('LOT : ' || lv_rtn_lot);

    

    --다중 insert 시 처리

    update T_TEST_TBL set

        lot = 'aaa'

    where lot is not null

    returning lot bulk collect into lt_rtn

    ;

    

    for cn in 1..lt_rtn.count loop

        dbms_output.put_line('lt_rtn(' || cn || ') : ' || lt_rtn(cn)); --update 후 결과가 return 됨

    end loop;

    

    --전체를 다 리턴받을 경우

    update T_TEST_TBL set

        WCODE                = '1023'        

        , STOCK             = 'STOCK'      

        , LOT               = 'LOT'        

    where lot is not null

    returning wcode, stock, lot, qty, pono, child_stock, child_lot, ref_seq, use_yn, addwho, adddate, editwho, editdate, prt_yn, asn_proc_yn bulk collect into lt_emi

--    returning wcode, stock, lot, qty, pono bulk collect into lt_emi /*record_type 과 동일한 값이 셋팅되어야 오류 안남*/ 

    ;

    

    for cn in lt_emi.first..lt_emi.last loop

        dbms_output.put_line('lt_rtn(' || cn || ') : ' || lt_emi(cn).wcode || ', ' || lt_emi(cn).lot || ', ' || lt_emi(cn).pono); --update 후 결과가 return 됨

    end loop;    

    

    /*record 활용*/

    update T_TEST_TBL set

        wcode = '1021'

        , pono = '4700001'

        , lot = 'LOTXXX'

    where lot is not null

    returning wcode, lot, pono bulk collect into lt_emi2

    ;

    

    for cn in lt_emi2.first..lt_emi2.last loop

        dbms_output.put_line('lt_emi2(' || cn || ') : ' || lt_emi2(cn).wcode || ', ' || lt_emi2(cn).lot || ', ' || lt_emi2(cn).pono); --update 후 결과가 return 됨

    end loop;

    

    

    --임의로 쿼리 수행 시 return

    --record type 으로 여러건 받는건 지원 안하는듯 

    execute immediate '

        update T_TEST_TBL set

            wcode = ''1021''

            , pono = ''4700001''

            , lot = ''LOTXXX''

        where lot is not null

        returning lot into :lt_rtn 

    '

    returning bulk collect into lt_rtn

    ;

    

    for cn in lt_rtn.first..lt_rtn.last loop

        dbms_output.put_line('lt_rtn : ' || lt_rtn(cn));

    end loop;

    

    commit;     

end;