Multi-way output (MULTI INSERT)
MaxCompute SQL lets you insert data into multiple destination tables or partitions in a single SQL statement. You can use insert into or insert overwrite to perform this multi-way output.
You can run the commands in this topic on the following platforms:
Prerequisites
Before you start, make sure you have Update permission on the destination table and Select permission on the source table. For more information about authorization, see MaxCompute permissions.
Function introduction
You can use a multi insert operation to insert data into multiple destination tables or partitions. This feature allows for multi-way output from a single data processing pass in MaxCompute SQL.
Limits
The multi insert operation has the following limits:
A single
multi insertstatement supports a maximum of 255 outputs. If you exceed this limit, a syntax error is reported.In a single
multi insertstatement, you cannot write to the same destination partition of a partitioned table more than once.In a single
multi insertstatement, you cannot write to the same non-partitioned table more than once.
Command format
from <from_statement>
insert overwrite | into table <table_name1> [partition (<pt_spec1>)]
<select_statement1>
insert overwrite | into table <table_name2> [partition (<pt_spec2>)]
<select_statement2>
...;from_statement: Required. The
fromclause that specifies the data source, such as the source table.table_name: Required. The name of the destination table.
pt_spec: Optional. Specifies the partitions where you want to insert data. Only constants are allowed. Expressions, such as functions, are not permitted. The format is
(partition_col1 = partition_col_value1, partition_col2 = partition_col_value2, ...). When you insert data into multiple partitions, such as pt_spec1 and pt_spec2, the partition information in pt_spec1 and pt_spec2 must be different.select_statement: Required. The
selectclause that selects the data to be inserted from the source table.
Usage examples
Example 1: Insert sales records for China from 2010 and 2011 from the sale_detail table into the sale_detail_multi table. The sample command is as follows:
--Create the sale_detail_multi table. create table sale_detail_multi like sale_detail; --Enable full table scan for the current session. Insert data from the sale_detail table into the sale_detail_multi table. set odps.sql.allow.fullscan=true; from sale_detail insert overwrite table sale_detail_multi partition (sale_date='2010', region='china' ) select shop_name, customer_id, total_price insert overwrite table sale_detail_multi partition (sale_date='2011', region='china' ) select shop_name, customer_id, total_price; --Enable full table scan for the current session. Execute a select statement to view the data in the sale_detail_multi table. set odps.sql.allow.fullscan=true; select * from sale_detail_multi; --The following result is returned. +------------+-------------+-------------+------------+------------+ | shop_name | customer_id | total_price | sale_date | region | +------------+-------------+-------------+------------+------------+ | s1 | c1 | 100.1 | 2010 | china | | s2 | c2 | 100.2 | 2010 | china | | s3 | c3 | 100.3 | 2010 | china | | s1 | c1 | 100.1 | 2011 | china | | s2 | c2 | 100.2 | 2011 | china | | s3 | c3 | 100.3 | 2011 | china | +------------+-------------+-------------+------------+------------+Example 2: You cannot write to the same destination partition more than once. This operation causes an error. The following is an incorrect command example:
from sale_detail insert overwrite table sale_detail_multi partition (sale_date='2010', region='china' ) select shop_name, customer_id, total_price insert overwrite table sale_detail_multi partition (sale_date='2010', region='china' ) select shop_name, customer_id, total_price;