Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert values statement can contain only constant literal values or variable references in SQL Data Warehouse

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?

like image 399
Lukas Eder Avatar asked Apr 20 '26 21:04

Lukas Eder


2 Answers

It appears that there are a few limitations on the INSERT .. VALUES statement of SQL Data Warehouse, but none on INSERT .. SELECT. The requested query can be rewritten to:

INSERT INTO t SELECT (SELECT 1), (SELECT 2);

This workaround is also useful when inserting multiple rows:

-- Doesn't work:
INSERT INTO t VALUES ((SELECT 1), 2), ((SELECT 2), 3), ...;

-- Works:
INSERT INTO t SELECT (SELECT 1), 2 UNION ALL SELECT (SELECT 2), 3;
like image 155
Lukas Eder Avatar answered Apr 22 '26 10:04

Lukas Eder


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.

like image 39
Matt Usher Avatar answered Apr 22 '26 09:04

Matt Usher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!