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

    Download PDF

    OceanBase logo

    The Unified Distributed Database for the AI Era.

    Follow Us
    Products
    OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
    Resources
    DocsBlogLive 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.0
    iconOceanBase Database
    SQL - V 4.2.0
    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

    INSERT

    Last Updated:2023-11-17 03:02:19  Updated
    share
    What is on this page
    Purpose
    Syntax
    Parameters
    Examples

    folded

    share

    Purpose

    You can use this statement to add one or more records to a table.

    Syntax

    INSERT [hint_options] { single_table_insert | multi_table_insert }
    
    single_table_insert:
        { INTO insert_table_clause opt_nologging '(' column_list ')' values_clause [{ RETURNING | RETURN } returning_exprs [into_clause]]
        | INTO insert_table_clause opt_nologging '(' ')' values_clause [{ RETURNING | RETURN } returning_exprs [into_clause]]
        | INTO insert_table_clause opt_nologging values_clause [{ RETURNING | RETURN } returning_exprs [into_clause]]
        }
    
    opt_nologging:
        { NOLOGGING | /*EMPTY*/ }
    
    returning_exprs:
        projection_col_name [,projection_col_name ...]
    
    insert_into_clause:
        { INTO into_var_list | BULK COLLECT INTO into_var_list}
    
    into_var_list:
        { USER_VARIABLE | ref_name } [, { USER_VARIABLE | ref_name }...]
    
    values_clause:
        VALUES ({ expr | DEFAULT } [, { expr | DEFAULT } ]...  )
    
    multi_table_insert:
        { ALL { insert_into_clause [ values_clause ] [error_logging_clause] }
        | conditional_insert_clause
        } subquery
    
    conditional_insert_clause:
        [ ALL | FIRST ]
        WHEN condition
        THEN insert_into_clause
        [ values_clause ]
        [ error_logging_clause ]
        [ insert_into_clause [ values_clause ] [ error_logging_clause ] ]...
        [ WHEN condition
            THEN insert_into_clause
            [ values_clause ]
            [ error_logging_clause ]
            [ insert_into_clause [ values_clause ] [ error_logging_clause ] ]...
        ]...
        [ ELSE insert_into_clause
            [ values_clause ]
            [ error_logging_clause ]
            [ insert_into_clause [ values_clause ] [ error_logging_clause ] ]...
        ]
    
    error_logging_clause:
        LOG ERRORS [ INTO [schema.] table_name ] [ (simple_expression) ] [ REJECT LIMIT { integer | UNLIMITED } ]
    

    Parameters

    Parameter
    Description
    hint_options (Optional) The hint options.
    single_table_insert Inserts data into a single table.
    insert_table_clause The table into which the records are inserted, which can be a base table, an updatable view, or a special subquery.
    opt_nologging Minimizes the number of logs generated by an insert.
    column_list The name of the column into which the records are inserted.
    returning_exprs Returns the projected column into which the records are inserted.
    insert_into_clause Inserts the column into which the records are inserted into the specified table.
    multi_table_insert Inserts data into multiple tables.
    conditional_insert_clause Conditionally inserts data into multiple tables.
    • ALL: traverses all the conditional branches and inserts data into the corresponding tables that meet the condition.
    • FIRST: inserts data into the table corresponding to the first conditional branch that is met and ignores the remaining branches.
    error_logging_clause Saves the SQL errors and the values in the columns of affected rows in an error record table.

    Notice

    A special subquery is similar to a subquery that defines an updatable view. These subqueries must not contain complex operators (such as GROUP BY, DISTINCT, and WINDOW FUNCTION).

    Examples

    Sample tables and their data are defined as follows:

    obclient> CREATE TABLE tbl1(col1 INT PRIMARY KEY, col2 INT,col3 INT);
    Query OK, 0 rows affected
    
    • Single-table insert: Insert a row into the tbl1 table.

      obclient> INSERT INTO tbl1 VALUES(1,2,3);
      Query OK, 1 row affected
      
      obclient> SELECT * FROM tbl1;
      +------+------+------+
      | COL1 | COL2 | COL3 |
      +------+------+------+
      |    1 |    2 |    3 |
      +------+------+------+
      1 row in set
      
    • Single-table insert: Insert data into a subquery.

      obclient>INSERT INTO (SELECT * FROM tbl1) VALUES(1,2,3);
      Query OK, 1 row affected
      
      obclient> SELECT * FROM tbl1;
      +----+------+------+
      | COL1 | COL2   | COL3   |
      +----+------+------+
      |  1 |    2 |    3 |
      +----+------+------+
      1 row in set
      
    • Single-table insert: Insert data into a table, with the RETURNING clause specified in the statement.

      obclient> INSERT INTO tbl1 VALUES(1,2,3) RETURNING col1;
      +------+
      | COL1 |
      +------+
      |    1 |
      +------+
      1 row in set
      
      obclient> SELECT * FROM tbl1;
      +------+------+------+
      | COL1 | COL2 | COL3 |
      +------+------+------+
      |    1 |    2 |    3 |
      +------+------+------+
      1 row in set
      
    • General multi-table insert: Insert one row (1,1,1) into the tbl1 table and one row (2,2,2) into the tbl2 table when the tbl3 table contains at least one row.

      obclient> INSERT ALL INTO tbl1 VALUES(1,1,1)
                                          INTO tbl2 VALUES(2,2,2)
                      SELECT * FROM tbl3 WHERE ROWNUM< 2;
      Query OK, 2 rows affected
      Records: 2  Duplicates: 0  Warnings: 0
      
      obclient> SELECT * FROM tbl1;
      +------+------+------+
      | COL1 | COL2 | COL3 |
      +------+------+------+
      |    1 |    1 |    1 |
      +------+------+------+
      1 row in set
      
      obclient> SELECT * FROM tbl2;
      +------+------+------+
      | COL1 | COL2 | COL3 |
      +------+------+------+
      |    2 |    2 |    2 |
      +------+------+------+
      1 row in set
      
    • Conditional multi-table insert: Use INSERT ALL to insert one row (1,1,1) into the tbl1 table when the value of col2 in the tbl table is greater than 1. Insert one row (2,2,2) into the tbl2 table when the value of col3 in the tbl table is greater than 1. Insert one row (3,3,3) into the tbl1 table when both conditions are not met.

      obclient> INSERT INTO tbl VALUES(1,2,3);
      Query OK, 1 row affected
      
      obclient>INSERT ALL
                    WHEN col2 > 1 THEN INTO tbl1 VALUES(1,1,1)
                    WHEN col3 > 1 THEN INTO tbl2 VALUES(2,2,2)
                    ELSE INTO tbl1 VALUES(3,3,3) SELECT col2,col3 FROM tbl;
      Query OK, 2 rows affected
      Records: 2  Duplicates: 0  Warnings: 0
      
      obclient> SELECT * FROM tbl1;
      +----+------+------+
      | COL1 | COL2   | COL3   |
      +----+------+------+
      |  1 |    1 |    1 |
      +----+------+------+
      1 row in set
      
      obclient> SELECT * FROM tbl2;
      +----+------+------+
      | COL1 | COL2   | COL3   |
      +----+------+------+
      |  2 |    2 |    2 |
      +----+------+------+
      1 row in set
      
    • Conditional multi-table insert: Use INSERT FIRST to insert one row (1,1,1) into the tbl1 table and one row (4,4,4) into the tbl2 table when the value of col2 in the tbl table is greater than 1. Insert one row (2,2,2) into the tbl2 table when the value of col3 in the tbl table is greater than 1. Insert one row (3,3,3) into the tbl1 table when both conditions are not met.

      obclient>INSERT INTO tbl VALUES(1,2,3);
      Query OK, 1 row affected
      
      obclient>INSERT ALL
                    WHEN col2 > 1 THEN INTO tbl1 VALUES(1,1,1) INTO tbl2 VALUES(4,4,4)
                    WHEN col3 > 1 THEN INTO tbl2 VALUES(2,2,2)
                    ELSE INTO tbl1 VALUES(3,3,3) SELECT col2,col3 FROM tbl;
      Query OK, 3 rows affected
      Records: 3  Duplicates: 0  Warnings: 0
      
      obclient> SELECT * FROM tbl1;
      +----+------+------+
      | COL1 | COL2   | COL3   |
      +----+------+------+
      |  1 |    1 |    1 |
      +----+------+------+
      1 row in set
      
      obclient> SELECT * FROM tbl2;
      +------+------+------+
      | COL1   | COL2   | COL3   |
      +------+------+------+
      |    4 |    4 |    4 |
      |    2 |    2 |    2 |
      +------+------+------+
      2 rows in set
      

    Previous topic

    DELETE
    Last

    Next topic

    MERGE
    Next
    What is on this page
    Purpose
    Syntax
    Parameters
    Examples