INSERT

statementsupportedQueries & DML

docs.snowflake.com ↗

Updates a table by inserting one or more rows into the table. The values inserted into each column in the table can be explicitly-specified or the results of a query.

Syntax

supportedpartialnot supportednot scored

INSERT [ OVERWRITE ] INTO <target_table> [ ( <target_col_name> [ , ... ] ) ]
       {
           VALUES ( { <value> | DEFAULT | NULL } [ , ... ] ) [ , ( ... ) ]
         | <query>
       }

To replace selected rows, use OVERWRITE with DELETING WHERE

INSERT OVERWRITE INTO <target_table> [ AS <target_alias> ]
       [ USING ( <using_query> ) [ AS ] <delta_alias> ]
       DELETING WHERE ( <predicate> )
       [ ( <target_col_name> [ , ... ] ) ]
       {
           VALUES ( { <value> | DEFAULT | NULL } [ , ... ] ) [ , ( ... ) ]
         | <query>
       }

Parameters

NameRequiredDescription
target_tablerequiredSpecifies the target table into which to insert rows.
VALUES ( value | DEFAULT | NULL [ , ... ] ) [ , ( ... ) ]requiredSpecifies one or more values to insert into the corresponding columns in the target table. In a VALUES clause, you can specify the following: value: Inserts the explicitly-specified value. The value can be a literal or an expression that evaluates to a single value. DEFAULT: Inserts the default value for the corresponding column in the target table. NULL: Inserts a NULL value. Each value in the clause must be separated by a comma. You can insert multiple rows by specifying additional sets of values in the clause. For more information, see the Usage notes and the Examples.
queryrequiredSpecify a query statement that returns values to be inserted into the corresponding columns. This allows you to insert rows into a target table from one or more source tables.
OVERWRITEoptionalSpecifies that existing rows are removed before new rows are inserted. Without DELETING WHERE, all existing rows are removed. With DELETING WHERE, only rows for which the predicate evaluates to TRUE are removed. Specifying this option doesn’t change the access control privileges granted on the table. INSERT statements with OVERWRITE can be processed within the scope of the current transaction, which avoids DDL statements that commit a transaction, such as:Copy code DROP TABLE t; CREATE TABLE t AS SELECT * FROM ... ; Default: No value (existing rows aren’t removed before performing the inserts).
AS target_aliasoptionalSpecifies an alias for the target table in a conditional overwrite statement. Use this alias to qualify target columns in DELETING WHERE. The AS keyword is required for this alias.
USING ( using_query ) [ AS ] delta_aliasoptionalDefines a relation that subqueries in DELETING WHERE and the insertion query can both reference. The relation must have an alias; AS before this alias is optional. The USING query can’t reference the target alias. The relation isn’t an implicit row source in the deletion predicate. For example, use t.id IN (SELECT id FROM delta), rather than t.id = delta.id.
DELETING WHERE ( predicate )optionalSpecifies which existing target rows to remove. The predicate must be a Boolean expression enclosed in parentheses. Rows for which the predicate evaluates to FALSE or NULL remain in the table. Deletion and insertion are independent: all rows returned by the insertion query are inserted, even if they don’t satisfy the deletion predicate. Matching target rows are removed even when the insertion query returns no rows. The predicate doesn’t apply to newly inserted rows. This clause requires OVERWRITE. If you specify a target column list, place it after DELETING WHERE.
( target_col_name [ , ... ] )optionalSpecifies one or more columns in the target table into which the corresponding values are inserted. The number of target columns specified must match the number of specified values or columns (if the values are the results of a query) in the VALUES clause. Default: No value (all the columns in the target table are updated).

Usage notes

See also