MERGE 를 사용해 보았는가?

준비물 : Table !!!

@ Table A
create table table_a
(
    id number
);

insert into table_a
select rownum
from all_tables
where rownum<7;

select * from table_a;
ID
-----------
          1
          2
          3
          4
          5
          6
6 rows selected.




@ Table B
create table table_b
(
    id number,
    status varchar2(255)    
);

insert into table_b values(1,'NEW');
insert into table_b values(3,'NEW');
insert into table_b values(5,'NEW');

select * from table_b;
ID                  STATUS
-----------   ------------------------------------------
          1                                    NEW
          3                                    NEW
          5                                    NEW
3 rows selected.



@@@ 결과
merge into table_b b
using ( select * from table_a ) a
on (a.id=b.id)
when matched then
    update set status='OLD'
when not matched then
    insert values(a.id, 'NEW');


ID                  STATUS
-----------   ------------------------------------------
          1                                    OLD
          3                                    OLD
          5                                    OLD
          2                                    NEW
          6                                    NEW
          4                                    NEW
6 rows selected.
    
훌륭하지 않습니까?
프로그래밍 코딩없이 이런기능을 할수있다는건. 아주 좋다고 생각됩니다!!

^_^


페이스북 댓글
티스토리 댓글

+ Recent posts