Consider this table:
CREATE TABLE t (i int, j int, ...);
I want to insert data into a table from a set of SELECT
statements. The simplified version of my query is:
INSERT INTO t VALUES ((SELECT 1), (SELECT 2), ...);
The real query can be much more complex, and the individual subqueries independent. Unfortunately, this standard SQL statement (which works on SQL Server) doesn't work on SQL Data Warehouse. The following error is raised:
Failed to execute query. Error: Insert values statement can contain only constant literal values or variable references.
Is there a way to work around this?
It appears that there are a few limitations on the
INSERT .. VALUES
statement of SQL Data Warehouse, but none onINSERT .. SELECT
. The requested query can be rewritten to:This workaround is also useful when inserting multiple rows:
You can also just run a CREATE TABLE AS SELECT (CTAS) statement. This gives you the full syntax support in the SELECT statement and control of the table shape (distribution type, index type) in the statement. A CTAS statement is fully parallalized.
Strange syntax, but it works. Here is a more complex example: