18

According to the Redshift WITH Clause documentation, you can use a WITH clause with a INSERT INTO...SELECT statement. However when testing this, I am getting the below error. Is this not possible, or do I have the syntax wrong?

CREATE TABLE TestCTEInsert (SomeTimestamp TIMESTAMP);
WITH CTE AS
(SELECT GETDATE() as SomeTimestamp)
INSERT INTO TestCTEInsert
    (SomeTimestamp) SELECT SomeTimestamp from CTE;

ERROR: 42601: syntax error at or near "insert"

Interestingly, it does support inserting into a new table i.e.

WITH CTE AS
(SELECT GETDATE() as SomeTimestamp)
INSERT SomeTimestamp INTO NewTable 
SELECT SomeTimestamp from CTE;

The command completed successfully (1 rows affected)

EDIT: Just to confirm, I get the same error when using an INTEGER column rather than TIMESTAMP:

CREATE TABLE TestCTE (SomeInt INTEGER);
WITH CTE AS
(SELECT 1 as SomeInt)
INSERT INTO TestCTEInsert 
SELECT SomeInt from CTE;

ERROR: 42601: syntax error at or near "insert"

fez
  • 1,726
  • 3
  • 21
  • 31
  • try removing (SomeTimestamp) in the insert – sagi Jan 18 '16 at 11:01
  • Thanks for your suggestion. This returns the same error: `ERROR: 42601: syntax error at or near "insert"` – fez Jan 18 '16 at 11:04
  • As far as i know, you cannot insert an explicit value into a timestamp column. – Akshey Bhat Jan 18 '16 at 11:09
  • Thanks @AksheyBhat but I don't think this is the problem here. I recreated this with an integer column rather than timestamp and got the same error message. – fez Jan 18 '16 at 11:12
  • The `;` goes at the **end** of a statement not somewhere in the middle. –  Jan 18 '16 at 11:44

4 Answers4

40

Try putting the CTE in the insert (not sure if that beats the point)

INSERT INTO TestCTEInsert
WITH CTE AS
(SELECT CURRENT_TIMESTAMP as SomeTimestamp)
SELECT SomeTimestamp from CTE;
Adeeb Armalite
  • 614
  • 6
  • 7
7

The ; terminates a statement, so it needs to go at the end of the statement, not somewhere in the middle:

You can do this in two ways, either use a create table as select

create table TestCTEInsert 
as
WITH CTE AS
(  
   SELECT current_timestamp as SomeTimestamp
)
SELECT SomeTimestamp 
from CTE; -- ; only at the end

Or in two steps:

CREATE TABLE TestCTEInsert (SomeTimestamp TIMESTAMP); -- end this with a ;

insert into TestCTEInsert
WITH CTE AS
(  
   SELECT current_timestamp as SomeTimestamp
)
SELECT SomeTimestamp 
from CTE; -- ; only at the end

The above runs on a vanilla Postgres installation, I don't have access to RDS

  • Thanks for your help @a_horse_with_no_name. The semicolon in my script was just to separate the CREATE TABLE and the CTE insert so I could do this in two steps (like your second query above). I have accepted Adeeb's answer as he was a little quicker with his answer. – fez Jan 18 '16 at 13:55
2

Change your script to this

  CREATE TABLE TestCTE (SomeInt INTEGER)
  WITH CTE AS (SELECT 1 as SomeInt) 
  INSERT INTO TestCTE  SELECT SomeInt from CTE;
user2460074
  • 1,252
  • 3
  • 11
  • 28
  • This returns the same error. `ERROR: 42601: syntax error at or near "insert"`. I recreated this with an integer column rather than timestamp and got the same error message. – fez Jan 18 '16 at 11:15
1

Try this

 CREATE TABLE TestCTE (SomeInt INTEGER)
;WITH CTE AS
(SELECT 1 as SomeInt)
INSERT (SomeInt) INTO TestCTE
SELECT SomeInt FROM CTE;
fez
  • 1,726
  • 3
  • 21
  • 31
Hiten004
  • 2,425
  • 1
  • 22
  • 34