INSERT (multi-table)¶
Updates multiple tables by inserting one or more rows with column values (from a query) into the tables. Supports both unconditional and conditional inserts.
- See also:
Syntax¶
Where:
Required parameters¶
ALLUnconditional multi-table insert only
Specifies that each row executes every
INTOclause in the INSERT statement.Note
If the
FIRSTkeyword is specified in an unconditional multi-table insert (or theALLkeyword is not specified), Snowflake returns a syntax error.FIRSTorALLConditional multi-table insert only
FIRSTSpecifies that each row executes only the first
WHENclause for which the condition evaluates to TRUE. If noWHENclause evaluates to TRUE, then theELSEclause, if present, executes.ALLSpecifies that each row executes all
WHENclauses. If noWHENclause evaluates to TRUE, then theELSEclause, if present, executes.
Note
A conditional multi-table insert must contain at least one
WHENclause.Each
WHENclause can contain multipleINTOclauses and theINTOclauses can insert into the same target table.To always execute a
WHENclause, use:WHEN 1=1 THEN ...
conditionConditional multi-table insert only
Specifies the condition that must evaluate to TRUE in order for the values specified in the
INTOclause to be inserted. The condition can be a SELECT list.target_tableSpecifies a target table into which to insert rows. The same table may be referenced more than once (in separate
WHENclauses).Multiple tables can be targeted by including a
INTOclause for each table.subquerySpecifies the SELECT list that determines the source of the values to be inserted into the target tables.
Optional parameters¶
OVERWRITESpecifies to truncate the target tables before inserting into the tables, while retaining access control privileges on the tables.
INSERT statements with
OVERWRITEcan be processed within the scope of the current transaction, which avoids DDL statements that commit a transaction, such as:Default: No value (the target tables are not truncated before performing the inserts)
( target_col_name [ , ... ] )Specifies one or more columns in the target table into which the values from the corresponding column in the source is inserted. The number of target columns specified must match the number of values specified in the source.
Default: No value (all the columns in the target table are updated)
VALUES ( source_col_name | DEFAULT | NULL [ , ... ] )Specifies one or more values to insert into the corresponding columns in the target table. The values can be:
source_col_name: Specifies the column in the source that contains the value to be inserted into the corresponding column in the target table.DEFAULT: Inserts the default value for the corresponding column in the target table.NULL: Inserts aNULLvalue.
Each value in the clause must be separated by a comma. Also, the number of values specified must match the number of columns specified for the target table.
Default: No value (values from all the columns in the source are inserted into the corresponding columns in the target table)
Usage notes¶
In an
INTOclause, theVALUESclause is optional. If it is omitted, the values from the SELECT list are inserted into the target table in their natural order.Expressions in
WHENclauses (for conditional multi-table inserts) andVALUESclauses can only reference the subquery via an alias. The alias must be one of the following:Explicit alias specified for a SELECT expression.
Default alias for an expression.
Positional alias ($1, $2, etc.).
In addition, columns and expressions of the subquery that are not in the outermost SELECT list can not be referenced in
WHENandVALUESclauses. For details, see Examples (in this topic).In each row produced by the
subquery, the value insource_col_namemust be compatible with the data type of the correspondingtarget_col_name. This rule applies even to rows that would be filtered out by theconditionin theWHENclause. The order of operations does not guarantee that the filter in theWHENclause is applied before the value insource_col_nameis evaluated for data type compatibility.
Examples¶
Unconditional multi-table inserts¶
Insert each row in the src table twice into tables t1 and t2. In this example, the inserted rows are not
identical; each of the inserted rows has different values/orders because we use the VALUES clause to vary the data:
Conditional multi-table inserts¶
The next two examples show how to create conditional multi-table inserts by
using WHEN clauses and an ELSE clause to decide which table(s), if any, each row is inserted into.
These examples also show the difference between using INSERT ALL and
INSERT FIRST.
Execute all WHEN clauses with an ELSE clause:
Rows where
n1 > 100also satisfy the conditionn1 > 10and are therefore inserted int1twice when theALLkeyword is used.Rows where
n1 <= 10satisfy theELSEcase and are inserted int2.
If the table src contains 3 rows, in which n1 has the values 1, 11, and 101, then after the INSERT statement the tables t1 and t2 will hold the values shown below:
t1:
101 |
101 > 100, so the first |
101 |
101 > 10, so the second |
11 |
11 > 10, so the second |
The row with n1 = 1 is not inserted into t1 because it does not satisfy
any WHEN clause that inserts into t1, and because the ELSE
clause does not insert into t1.
t2:
101 |
101 > 10, so the second |
11 |
11 > 10, so the second |
1 |
the row didn’t satisfy any of the |
The next example is similar to the previous example, except with a FIRST clause.
If the table src contains 3 rows, in which n1 has the values 1, 11, and 101, then after the INSERT statement the tables t1 and t2 will hold the values shown below:
t1:
101 |
101 > 100, so the first |
11 |
11 > 10, so the second |
The row with n1 = 1 is not inserted into t1 because it does not satisfy any WHEN clause that inserts into t1, and because
the ELSE clause does not insert into t1.
Unlike in the previous example, which used ALL, the row with n1 = 101 is inserted into t1 only once because the first
WHEN clause evaluates to TRUE so the second WHEN clause is ignored.
t2:
11 |
11 > 10, so the second |
1 |
the row didn’t satisfy any of the |
The row n1 = 101 is not inserted into t2 because 101 is greater than 100, so it matches the first WHEN clause, but the
first WHEN clause doesn’t insert into t2, and the statement doesn’t check any of the other WHEN clauses or use the
ELSE clause because the row already qualified for the first WHEN clause.
Multi-table inserts with aliases and references¶
Insert values using a positional alias ($1), an explicit alias (an_alias), and a default alias ("10 + 20");
this example inserts a single row with values (1, 50, 30) into table t1:
Illustrate inserting values from columns that must be selected to be referenced (b and c in table src):
Illustrate inserting values from a column that cannot be referenced (src1.key); instead, it must be selected and aliased: