OceanBase logo

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Resources

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS

OceanBase Cloud

OceanBase Database

Tools

Connectors and Middleware

QUICK START

OceanBase Cloud

OceanBase Database

BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Company

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

International - English
中国站 - 简体中文
日本 - 日本語
Sign In
Start on Cloud

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS
OceanBase CloudOceanBase Database
ToolsConnectors and Middleware
QUICK START
OceanBase CloudOceanBase Database
BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

Start on Cloud
编组
All Products
    • Databases
    • iconOceanBase Database
    • iconOceanBase Cloud
    • iconOceanBase Tugraph
    • iconInteractive Tutorials
    • iconOceanBase Best Practices
    • Tools
    • iconOceanBase Cloud Platform
    • iconOceanBase Migration Service
    • iconOceanBase Developer Center
    • iconOceanBase Migration Assessment
    • iconOceanBase Admin Tool
    • iconOceanBase Loader and Dumper
    • iconOceanBase Deployer
    • iconKubernetes operator for OceanBase
    • iconOceanBase Diagnostic Tool
    • iconOceanBase Binlog Service
    • Connectors and Middleware
    • iconOceanBase Database Proxy
    • iconEmbedded SQL in C for OceanBase
    • iconOceanBase Call Interface
    • iconOceanBase Connector/C
    • iconOceanBase Connector/J
    • iconOceanBase Connector/ODBC
    • iconOceanBase Connector/NET
icon

OceanBase Database

SQL - V4.2.2

    Download PDF

    OceanBase logo

    The Unified Distributed Database for the AI Era.

    Follow Us
    Products
    OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
    Resources
    DocsBlogWhite PaperLive DemosTraining & CertificationTicket
    Company
    About OceanBaseTrust CenterLegalPartnerContact Us
    Follow Us

    © OceanBase 2026. All rights reserved

    Cloud Service AgreementPrivacy PolicySecurity
    Contact Us
    Document Feedback
    1. Documentation Center
    2. OceanBase Database
    3. SQL
    4. V4.2.2
    iconOceanBase Database
    SQL - V 4.2.2
    Databases
    • OceanBase Database
    • OceanBase Cloud
    • OceanBase Tugraph
    • Interactive Tutorials
    • OceanBase Best Practices
    Tools
    • OceanBase Cloud Platform
    • OceanBase Migration Service
    • OceanBase Developer Center
    • OceanBase Migration Assessment
    • OceanBase Admin Tool
    • OceanBase Loader and Dumper
    • OceanBase Deployer
    • Kubernetes operator for OceanBase
    • OceanBase Diagnostic Tool
    • OceanBase Binlog Service
    Connectors and Middleware
    • OceanBase Database Proxy
    • Embedded SQL in C for OceanBase
    • OceanBase Call Interface
    • OceanBase Connector/C
    • OceanBase Connector/J
    • OceanBase Connector/ODBC
    • OceanBase Connector/NET
    SQL
    KV
    • V 4.6.0
    • V 4.4.2
    • V 4.3.5
    • V 4.3.3
    • V 4.3.1
    • V 4.3.0
    • V 4.2.5
    • V 4.2.2
    • V 4.2.1
    • V 4.2.0
    • V 4.1.0
    • V 4.0.0
    • V 3.1.4 and earlier

    MERGE

    Last Updated:2026-04-15 08:27:15  Updated
    Share
    What is on this page
    Purpose
    Syntax
    Parameters
    Examples

    folded

    Share

    Purpose

    You can use this statement to insert, update, or delete rows in a target table based on the source table.

    Syntax

    MERGE [hint_options] INTO target_table_name [opt_alias]
        USING source_table_name [opt_alias]
        ON (expr)
        [merge_update_clause] [merge_insert_clause]
    
    merge_update_clause:
        WHEN MATCHED THEN UPDATE SET update_asgn_list [WHERE expr] [DELETE WHERE expr]
    
    merge_insert_clause:
        WHEN NOT MATCHED THEN INSERT opt_insert_columns VALUES '(' insert_vals ')' [WHERE expr]
    

    Parameters

    Parameter
    Description
    hint_options Optional. The hint options.
    target_table_name The name of the target table where rows are to be updated or inserted.
    source_table_name The name of the source table.
    ON (expr) The condition for joining the source and target tables.
    update_asgn_list Updates the value assignment operation for the statement.
    WHERE expr The conditions for triggering an update, delete, or insert operation.

    Examples

    Sample tables and their data are defined as follows:

    obclient> CREATE TABLE tbl1 (col1 INT, col2 INT);
    Query OK, 0 rows affected
    
    obclient> CREATE TABLE tbl2 (col1 INT, col2 INT);
    Query OK, 0 rows affected
    
    obclient> INSERT INTO tbl1 VALUES(0, 0),(1, null),(2, null);
    Query OK, 3 row affected
    
    obclient> INSERT INTO tbl2 VALUES(1, 1),(2, 20),(3, 3),(4, 40);
    Query OK, 4 row affected
    
    obclient> SELECT * FROM tbl1;
    +------+------+
    | COL1 | COL2 |
    +------+------+
    |    0 |    0 |
    |    1 | NULL |
    |    2 | NULL |
    +------+------+
    3 rows in set
    
    obclient> SELECT * FROM tbl2;
    +------+------+
    | COL1 | COL2 |
    +------+------+
    |    1 |    1 |
    |    2 |   20 |
    |    3 |    3 |
    |    4 |   40 |
    +------+------+
    4 rows in set
    

    Update the data of tbl1 based on the data of tbl2.

    • For each row where the condition tbl1.col1 = tbl2.col1 is true in tbl1, OceanBase Database updates the row with the corresponding value of tbl2.col2 if tbl1.col2 is empty in the row. If the updated tbl1.col2 is greater than or equal to 10, the row is deleted.

    • If the condition tbl1.col1 = tbl2.col1 is not true for any rows in tbl1, OceanBase Database inserts the unmatched rows of tbl2 into tbl1, with the prerequisite that tbl2.col2 in the unmatched rows is smaller than 10.

    obclient>MERGE INTO tbl1 USING tbl2 ON (tbl1.col1 = tbl2.col1)
           WHEN MATCHED THEN UPDATE SET tbl1.col2 = tbl2.col2 WHERE tbl1.col2 IS NULL DELETE
           WHERE tbl1.col2 >= 10
           WHEN NOT MATCHED THEN INSERT VALUES(tbl2.col1, tbl2.col2)
           WHERE tbl2.col2 < 10;
    Query OK, 3 rows affected
    
    obclient> SELECT * FROM tbl1;
    +------+------+
    | COL1 | COL2 |
    +------+------+
    |    0 |    0 |
    |    1 |    1 |
    |    3 |    3 |
    +------+------+
    3 rows in set
    

    Previous topic

    INSERT
    Last

    Next topic

    PURGE
    Next
    What is on this page
    Purpose
    Syntax
    Parameters
    Examples