MERGE

- MERGE문은 특정 테이블에 대해 조건에 따라 삽입,갱신,삭제 작업을 한번에 처리할 수 있다.

 

merge
into t_test A
using (select 13 no from dual) B
on (A.no = B.no)
when matched then
  update set name = '개발자133'
  delete where (parent_no = 3)
when not matched then
  insert (no, name, parent_no)
  values(14, '개발자14', 3);

 

 

- INTO : DATA UPDATE되거나 INSERT 될 테이블 또는 뷰를 지정.

- USING : 비교할 테이블 또는 뷰를 지정.

  자기자신(INTO절에서 정의한 뷰 또는 테이블)과 비교하려면 아래와 같이 

  using dual 구문을 사용하도록한다.

merge
into t_test A
using dual

on (A.no = 13)
when matched then
  update set name = '개발자133'
  delete where (parent_no = 3)
when not matched then
  insert (no, name, parent_no)
  values(14, '개발자14', 3);

- ON : 비교할 조건을 명시 

- WHEN MATCHED : ON 조건절이 TRUE ROW에 수행 할 작업을 명시

  * update, delete만 명시 가능하다.

  * on 절에서 조건으로 사용된 칼럼은 update, delete 대상이 될 수 없다.

  * update절은 단독으로 사용가능하나, delete절은 단독으로 사용할 수 없다.

- WHEN NOT MATCHED : ON 조건절에 맞는 ROW가 없을 때 수행할 내용

  * insert만 명시 가능하다.

Posted by mypiece
,