Excel Add-In for Amazon Athena

Build 25.0.9539

INSERT INTO SELECT Statements

Use INSERT INTO SELECT queries to select a list of records from one table and insert those same records into another table as a group. Inserting batches of records in this way may result in improved query performance compared to using many individual INSERT INTO queries.

The table whose records are selected for insertion into another table can be either a real table or a user-defined temporary table.

Inserting Records from Real Tables

To insert a group of records from from one real, non-temporary, source table into another destination table, you can use an INSERT INTO SELECT query. This type of query is formatted similarly to a standard INSERT INTO query, except the VALUES clause is substituted with a SELECT query targeting the source table. All records matched by the embedded SELECT query are inserted into the destination table.

If the source table and destination tables have different column names, you must map columns from the source table to the corresponding columns in the destination table you want to insert them into. Perform this mapping by specifying the destination table columns in the same order as the source table columns you want to match them with. For example:

INSERT INTO DestinationTable (A,B,C,D) SELECT Q,R,S,T FROM SourceTable

In this example, the first source column (Q) is inserted into the first destination column (A), the second source column (R) is inserted into the second destination column (B), and so on.

If the source table and destination table both have the same column list with the same names, you can use a streamlined query.

INSERT INTO DestinationTableWithSameColumns SELECT * FROM SourceTable

In this example, there is no need to specify a list of columns for either the source or destination table, because their metadata already matches.

Inserting Records from Temporary Tables

You can manually define and populate temporary tables to hold a list of records for later bulk insertion.

Populate the Temporary Table

To create a temporary table, you must give it a name ending in "#TEMP" and execute an INSERT INTO query using that name, as if that table already existed in the database. After executing the first INSERT INTO, the temporary table exists and can receive subsequent INSERTs. For example:

INSERT INTO [AwsDataCatalog].[sampledb].Customers#TEMP (TotalDue, MyCustomField__c) VALUES ('New Customers', '9000');
INSERT INTO [AwsDataCatalog].[sampledb].Customers#TEMP (TotalDue, MyCustomField__c) VALUES ('New Customers 2', '9001');
INSERT INTO [AwsDataCatalog].[sampledb].Customers#TEMP (TotalDue, MyCustomField__c) VALUES ('New Customers 3', '9002');

This creates a temporary table called [AwsDataCatalog].[sampledb].Customers#TEMP with two columns and three rows of data. Since type cannot be determined on the temporary table itself, all values are considered strings and later converted to the proper type when they are inserted together into the real (non-temporary) table of interest.

Insert Temporary Table Contents into Real Tables

Once your temporary table is populated, execute an INSERT INTO SELECT query targeting the real (non-temporary) table you want to insert the temporary table's records into. This is formatted similarly to a standard INSERT INTO query, except the VALUES clause is substituted with a SELECT query targeting the matching columns in the temporary table. For example:

INSERT INTO [AwsDataCatalog].[sampledb].Customers (TotalDue, MyCustomField__c) SELECT TotalDue, MyCustomField__c FROM [AwsDataCatalog].[sampledb].Customers#TEMP
In this example, the full contents of [AwsDataCatalog].[sampledb].Customers#TEMP are inserted into the [AwsDataCatalog].[sampledb].Customers.

Results

The LastResultInfo#TEMP temporary table contains details about the most recently executed query that uses the contents of a temporary table in an embedded SELECT clause, as is the case for INSERT INTO SELECT queries that use a temporary table as the source of records. This table is cleared and repopulated each time such a query is executed. LastResultInfo#TEMP includes information such as whether the query in question succeeded, and how many rows were affected by the query.

Temporary Table Lifespan

Temporary tables only last as long as the connection remains open. When the connection to Amazon Athena is closed, all temporary tables are cleared, including the LastResultInfo#TEMP table.

Copyright (c) 2026 CData Software, Inc. - All rights reserved.
Build 25.0.9539