According to the Redshift WITH Clause directive, you can use the WITH clause with a statement INSERT INTO...SELECT. However, checking this, I get the following error. Is this not possible, or do I have the wrong syntax?
CREATE TABLE TestCTEInsert (SomeTimestamp TIMESTAMP);
WITH CTE AS
(SELECT GETDATE() as SomeTimestamp)
INSERT INTO TestCTEInsert
(SomeTimestamp) SELECT SomeTimestamp from CTE;
ERROR: 42601: syntax error in or near "insert"
Interestingly, it supports insertion into a new table i.e.
WITH CTE AS
(SELECT GETDATE() as SomeTimestamp)
INSERT SomeTimestamp INTO NewTable
SELECT SomeTimestamp from CTE;
Command completed successfully (1 row affected)
The EDIT: . To confirm, I get the same error when using a column INTEGER, not TIMESTAMP:
CREATE TABLE TestCTE (SomeInt INTEGER);
WITH CTE AS
(SELECT 1 as SomeInt)
INSERT INTO TestCTEInsert
SELECT SomeInt from CTE;
ERROR: 42601: syntax error in or near "insert"
source
share