> For the complete documentation index, see [llms.txt](https://docs.tibero.com/tibero-manuals/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tibero.com/tibero-manuals/7.2.6.manuals/tibero-sql-reference-guide/data-manipulation-language/merge.md).

# MERGE

MERGE 구문은 하나 또는 그 이상의 원본(Source) 데이터로부터 ROW를 선택하여 테이블에 삽입 또는 갱신 작업을 수행합니다. 삽입 작업과 갱신 작업 중 어떤 작업을 수행할지 결정하는 조건을 명시할 수 있습니다.

MERGE는 여러 작업을 통합할 수 있는 편리한 방법입니다. MERGE를 사용하면 여러 삽입, 갱신, 삭제 작업을 따로 수행해야 하는 수고를 덜 수 있습니다. 하나의 MERGE 문에서 같은 ROW를 여러 번 갱신할 수는 없습니다.

MERGE의 세부 내용은 다음과 같습니다.

* 문법

<div align="left"><figure><img src="/files/w7fasmELZeJIJG1zVteM" alt="" width="563"><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="/files/JgiuwJWJVThAWeTCYPSu" alt="" width="563"><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="/files/uGdBYzX7RM8zhWSrKVIi" alt="" width="498"><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="/files/pw2cPfmDOO7HF9E0jV1w" alt="" width="175"><figcaption></figcaption></figure></div>

* 특권
  * MERGE를 수행하기 위해서는 대상(Target) 데이터에 대한 INSERT 스키마 오브젝트 특권과 UPDATE 스키마 오브젝트 특권을 가지고 있어야 하며, 원본 데이터에 대한 SELECT 스키마 오브젝트 특권을 가지고 있어야 합니다.
  * merge\_update\_clause에 DELETE 절을 명시하기 위해서는 대상 데이터에 대한 DELETE 스키마 오브젝트 특권도 가지고 있어야 합니다.<br>
* 구성요소
  * merge

<table><thead><tr><th width="202">구성요소</th><th>설명</th></tr></thead><tbody><tr><td>hint</td><td>힌트를 명시</td></tr><tr><td>INTO</td><td>데이터를 삽입 또는 갱신할 테이블을 지정</td></tr><tr><td>schema</td><td>스키마 이름을 명시</td></tr><tr><td>alias</td><td>별칭을 명시</td></tr><tr><td>USING</td><td><ul><li>삽입 또는 갱신할 원본 데이터를 명시하기 위해 USING 절을 사용</li><li>원본 데이터로는 테이블, 뷰 또는 부질의 결과를 사용할 수 있음</li></ul></td></tr><tr><td>table, view, subquery</td><td>원본 데이터로 사용할 테이블, 뷰, 부질의를 명시</td></tr><tr><td>ON (condition)</td><td><ul><li>MERGE 작업이 수행될 조건을 명시하기 위해 ON 절을 사용</li><li>condition 부분에 조건식을 명시</li><li>대상 데이터 테이블의 각 ROW에 대해 조건이 TRUE이면 원본 데이터의 대응하는 ROW로 갱신</li><li>조건이 FALSE이면 대응하는 원본 데이터 테이블의 ROW를 대상 데이터 테이블에 삽입</li></ul></td></tr><tr><td>merge_update_clause</td><td><ul><li>대상 데이터 테이블의 새 컬럼 값을 명시</li><li>ON 절이 참일 경우 갱신을 수행</li><li>갱신이 실행되면 대상 데이터 테이블에 설정된 모든 갱신 트리거가 동작함</li><li>merge_update_clause는 단독으로 명시하거나 merge_insert_clause와 함께 명시할 수 있음</li><li>두 가지를 모두 명시할 경우 순서는 상관없음</li><li>ON 절에서 참조한 컬럼은 merge_update_clause에서 갱신할 수 없음</li></ul></td></tr><tr><td>merge_insert_clause</td><td><ul><li>ON 절의 조건이 거짓일 경우 대상 데이터 테이블에 삽입할 값을 명시</li><li>삽입이 실행되면 대상 데이터 테이블에 정의된 모든 삽입 트리거가 동작함</li><li>모든 원본 데이터의 ROW를 삽입하려면 ON 절 조건에 0=1과 같이 항상 거짓으로 평가되는 필터를 사용하면 됨</li><li>이 방법은 merge_update_clause를 생략하는 것과 다름</li><li>merge_update_clause를 생략하는 경우 데이터베이스는 조인을 수행하지만, 필터를 사용할 경우 조인을 하지 않고 무조건 삽입을 수행</li><li>merge_insert_clause는 단독으로 명시하거나 merge_update_clause와 함께 명시할 수 있음</li><li>두 가지를 모두 명시할 경우 순서는 상관없음</li></ul></td></tr></tbody></table>

\- merge\_update\_clause

<table><thead><tr><th width="222">구성요소</th><th>설명</th></tr></thead><tbody><tr><td>column</td><td>갱신할 컬럼의 이름을 명시</td></tr><tr><td>expr</td><td>컬럼 값을 갱신하는 데 사용할 임의의 연산식</td></tr><tr><td>DEFAULT</td><td>컬럼 값을 기본값으로 갱신할 때 사용</td></tr><tr><td>where_clause</td><td><ul><li>갱신 조건을 설정하기 위해 where_clause를 명시</li><li>원본 데이터에 대한 조건과 대상 데이터에 대한 조건을 모두 명시할 수 있음</li></ul></td></tr><tr><td>DELETE where_clause</td><td><ul><li>갱신 중 데이터를 삭제하기 위해 DELETE where_clause를 명시</li><li>DELETE where_clause에 의해 삭제되는 ROW는 갱신에 의해 영향을 받은 ROW뿐임</li><li>ROW가 삭제되면 설정된 모든 삭제 트리거가 동작함</li></ul></td></tr></tbody></table>

* merge\_insert\_clause

<table><thead><tr><th width="197">구성요소</th><th>설명</th></tr></thead><tbody><tr><td>column</td><td><ul><li>삽입할 컬럼의 이름을 명시</li><li>이 부분을 생략하면 대상 데이터 테이블의 컬럼 수는 VALUES 절에 명시된 값의 수와 일치해야 함</li></ul></td></tr><tr><td>expr</td><td>컬럼 값을 삽입하는 데 사용할 임의의 연산식</td></tr><tr><td>DEFAULT</td><td>컬럼 값을 기본값으로 삽입할 때 사용</td></tr><tr><td>where_clause</td><td><ul><li>특정 조건을 만족하는 ROW만 삽입할 때 where_clause를 명시</li><li>이 조건은 원본 데이터 테이블의 값만 참조할 수 있음</li></ul></td></tr></tbody></table>

* where\_clause

<table><thead><tr><th width="211">구성요소</th><th>설명</th></tr></thead><tbody><tr><td>condition</td><td><ul><li>갱신 조건이나 삽입 조건을 설정하기 위해 where_clause를 명시</li><li>condition에는 조건식을 명시</li></ul></td></tr></tbody></table>

* 예제

다음은 **MERGE**를 사용하는 예입니다.

```
CREATE TABLE BONUS (
    PNUM VARCHAR2(10) PRIMARY KEY,
    BONUS NUMBER(10,2)              
);

CREATE TABLE PERSONNEL (
    PNUM VARCHAR2(10) PRIMARY KEY,
    SALARY NUMBER(10,2)           
);

MERGE INTO BONUS B USING PERSONNEL P ON (B.PNUM = P.PNUM)
WHEN MATCHED THEN UPDATE SET B.BONUS = B.BONUS * 1.5
DELETE WHERE (P.SALARY > 3000)
WHEN NOT MATCHED THEN INSERT VALUES (P.PNUM, P.SALARY * 0.2);
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.tibero.com/tibero-manuals/7.2.6.manuals/tibero-sql-reference-guide/data-manipulation-language/merge.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
