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.6.0

    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.6.0
    iconOceanBase Database
    SQL - V 4.6.0
    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

    Replace data

    Last Updated:2026-05-07 11:26:24  Updated
    Share
    What is on this page
    Prerequisites
    Replace data in a table
    Records in the source table do not exist in the destination table
    Records in the source table exist in the destination table
    Use a DBLink to replace data
    References

    folded

    Share

    You can use the MERGE INTO statement to insert records to or update records in a table.

    Prerequisites

    Before you replace data in a table, make sure that:

    • You have connected to an Oracle tenant of OceanBase Database. For more information about how to connect to OceanBase Database, see Connection methods.

      Note

      You can query the oceanbase.DBA_OB_TENANTS view in the sys tenant to confirm the mode of the tenant to which you have logged on.

    • You have the SELECT object privilege on the source table, and the INSERT and UPDATE privileges on the destination table. For more information about how to view your privileges, see View user privileges. If you do not have the required privileges, contact the administrator to obtain the privileges. For more information, see Grant direct privileges.

    Replace data in a table

    Use the MERGE INTO statement to replace one or more records in a table.

    The syntax is as follows:

    MERGE INTO table_name alias1
    USING (table|view|sub_query) alias2
    ON (join condition)
    WHEN MATCHED THEN
        UPDATE table_name
        SET col1 = col1_val,
            col2 = col2_val
    WHEN NOT MATCHED THEN
        INSERT (column_list) VALUES (column_values);
    
    Parameter
    Required?
    Description
    MERGE INTO table_name alias1 Yes The name and alias of the table into which data is to be inserted.
    USING (table|view|sub_query) alias2 Yes The source table (view, subquery) and its alias.
    ON (join condition) No The judgment condition.
    WHEN MATCHED THEN UPDATE table_name SET col1 = col_val1,col2 = col2_val Yes Specifies to execute the UPDATE statement when the specified condition is matched.
    WHEN NOT MATCHED THEN INSERT (column_list) VALUES (column_values); Yes Specifies to execute the INSERT statement when the specified condition is not matched.

    When you use a MERGE INTO statement to replace data, if the record in the source table does not exist in the destination table, data is inserted into the destination table. Otherwise, the existing record in the destination table is updated.

    Records in the source table do not exist in the destination table

    If the records in the source table do not exist in the destination table, you can use the REPLACE INTO statement to insert these records into the destination table.

    The information about the source t_merge table is as follows:

    obclient [SYS]> CREATE TABLE t_merge(
        id  number NOT NULL PRIMARY KEY,
        name  varchar2(50)    NOT NULL,
        value  number,
        gmt_create  date NOT NULL DEFAULT sysdate,
        gmt_modified   date NOT NULL DEFAULT sysdate
    );
    Query OK, 0 rows affected
    
    obclient [SYS]> INSERT INTO t_merge(id,name,value) VALUES(4,'JP',10004),(5,'CN',10005);
    Query OK, 2 rows affected
    Records: 2  Duplicates: 0  Warnings: 0
    
    obclient [SYS]> SELECT * FROM t_merge;
    +----+------+-------+------------+--------------+
    | ID | NAME | VALUE | GMT_CREATE | GMT_MODIFIED |
    +----+------+-------+------------+--------------+
    |  4 | JP   | 10004 | 31-OCT-22  | 31-OCT-22    |
    |  5 | CN   | 10005 | 31-OCT-22  | 31-OCT-22    |
    +----+------+-------+------------+--------------+
    2 rows in set
    

    Example 1: Use the MERGE INTO statement to insert the data in the t_merge table into the t_insert table.

    obclient [SYS]> SELECT * FROM t_insert;
    +----+------+-------+------------+
    | ID | NAME | VALUE | GMT_CREATE |
    +----+------+-------+------------+
    |  1 | CN   | 10001 | 31-OCT-22  |
    |  2 | US   | 10002 | 31-OCT-22  |
    |  3 | EN   | 10003 | 31-OCT-22  |
    +----+------+-------+------------+
    3 rows in set
    
    obclient [SYS]> MERGE INTO t_insert a
     USING (SELECT id,name,value,gmt_create FROM t_merge) b
     ON (a.id = b.id)
     WHEN MATCHED THEN
         UPDATE SET a.name = b.name, a.value = b.value, a.gmt_create = sysdate
     WHEN NOT MATCHED THEN
         INSERT (a.id,a.name,a.value) VALUES(b.id, b.name, b.value);
    Query OK, 3 rows affected
    
    obclient [SYS]> SELECT * FROM t_insert;
    +----+------+-------+------------+
    | ID | NAME | VALUE | GMT_CREATE |
    +----+------+-------+------------+
    |  1 | CN   | 10001 | 31-OCT-22  |
    |  2 | US   | 10002 | 31-OCT-22  |
    |  3 | EN   | 10003 | 31-OCT-22  |
    |  4 | JP   | 10004 | 31-OCT-22  |
    |  5 | CN   | 10005 | 31-OCT-22  |
    +----+------+-------+------------+
    5 rows in set
    

    In this example, the id column in the destination t_insert table is the primary key column and must meet the UNIQUE constraint. The records (4,'JP',10004) and (5,'CN',10005) in the source table do not exist in the destination table, and therefore are inserted into the destination table.

    Records in the source table exist in the destination table

    If the records in the source table exist in the destination table, you can use the MERGE INTO statement to update these records in the destination table.

    The information about the source t_merge table is as follows:

    obclient [SYS]> CREATE TABLE t_merge(
        id  number NOT NULL PRIMARY KEY,
        name  varchar2(50)    NOT NULL,
        value  number,
        gmt_create  date NOT NULL DEFAULT sysdate,
        gmt_modified   date NOT NULL DEFAULT sysdate
    );
    Query OK, 0 rows affected
    
    obclient [SYS]> INSERT INTO t_merge(id,name,value) VALUES(3,'CN',10003),(4,'JP',10004),(5,'CN',10005);
    Query OK, 2 rows affected
    Records: 2  Duplicates: 0  Warnings: 0
    
    obclient [SYS]> SELECT * FROM t_merge;
    +----+------+-------+------------+--------------+
    | ID | NAME | VALUE | GMT_CREATE | GMT_MODIFIED |
    +----+------+-------+------------+--------------+
    |  3 | CN   | 10003 | 01-NOV-22  | 01-NOV-22    |
    |  4 | JP   | 10004 | 01-NOV-22  | 01-NOV-22    |
    |  5 | CN   | 10005 | 01-NOV-22  | 01-NOV-22    |
    +----+------+-------+------------+--------------+
    3 rows in set
    

    Example 2: Use the MERGE INTO statement to update the data in the name and value columns of the t_insert table.

    obclient [SYS]> SELECT * FROM t_insert;
    +----+------+-------+------------+
    | ID | NAME | VALUE | GMT_CREATE |
    +----+------+-------+------------+
    |  1 | CN   | 10001 | 31-OCT-22  |
    |  2 | US   | 10002 | 31-OCT-22  |
    |  3 | EN   | 10003 | 31-OCT-22  |
    +----+------+-------+------------+
    3 rows in set
    
    obclient [SYS]> MERGE INTO t_insert a
     USING (SELECT id,name,value,gmt_create FROM t_merge ) b
     ON (a.id = b.id)
     WHEN MATCHED THEN
         UPDATE SET a.name = b.name, a.value = b.value, a.gmt_create = sysdate
     WHEN NOT MATCHED THEN
         INSERT (a.id,a.name,a.value) VALUES(b.id, b.name, b.value);
    Query OK, 3 rows affected
    
    obclient [SYS]> SELECT * FROM t_insert;  
    +----+------+-------+------------+
    | ID | NAME | VALUE | GMT_CREATE |
    +----+------+-------+------------+
    |  1 | CN   | 10001 | 31-OCT-22  |
    |  2 | US   | 10002 | 31-OCT-22  |
    |  3 | CN   | 10003 | 01-NOV-22  |
    |  4 | JP   | 10004 | 01-NOV-22  |
    |  5 | CN   | 10005 | 01-NOV-22  |
    +----+------+-------+------------+
    5 rows in set
    

    In this example, the id column in the destination table t_insert is the primary key column and must meet the UNIQUE constraint. The (3,'CN',10003) record in the source t_merge table already exists in the destination table t_insert, and therefore is updated in the destination table.

    Use a DBLink to replace data

    The current OceanBase Database version allows you to use a DBLink to write data into OceanBase Database's Oracle mode and Oracle Database.

    The following example shows how to replace data of the t3 table in the remote database connected through a DBLink:

    obclient> SELECT * FROM t3@orcl_dblink;
    +------+------+
    | C1   | C2   |
    +------+------+
    |    3 |    3 |
    |    4 |    4 |
    +------+------+
    2 row in set
    
    obclient> SELECT * FROM t4;
    +------+------+
    | C1   | C2   |
    +------+------+
    |    5 |    5 |
    |    6 |    6 |
    +------+------+
    2 row in set
    
    obclient> MERGE INTO t3@orcl_dblink a
     USING (SELECT C1,C2 FROM t4 ) b
     ON (a.C1 = b.C1)
     WHEN MATCHED THEN
         UPDATE SET a.C2 = b.C2
     WHEN NOT MATCHED THEN
         INSERT (a.C1,a.C2) VALUES(b.C1, b.C2);
    Query OK, 2 rows affected
    
    obclient> commit;
    Query OK, 0 rows affected
    
    obclient> SELECT * FROM t3@orcl_dblink;
    +------+------+
    | C1   | C2   |
    +------+------+
    |    3 |    3 |
    |    4 |    4 |
    |    5 |    5 |
    |    6 |    6 |
    +------+------+
    4 row in set
    

    In the Oracle mode of OceanBase Database, you can use the INSERT, DELETE, UPDATE, and MERGE INTO statements to write data from local tables to remote tables. For more information, see Use a DBLink to modify data in a remote database.

    References

    • Insert data

    • Update data

    • Delete data

    • Use a DBLink to modify data in a remote database

    Previous topic

    Delete data
    Last

    Next topic

    Generate test data in batches
    Next
    What is on this page
    Prerequisites
    Replace data in a table
    Records in the source table do not exist in the destination table
    Records in the source table exist in the destination table
    Use a DBLink to replace data
    References