Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is this syntax invalid in postgresql?

I’m trying to insert some columns in a new table and set the ids in another one. I know this can be achieved using the WITH AS statement but I’m curious why it’s not working this way

UPDATE "a" 
SET "c1" = cp.id 
FROM (
    INSERT INTO "b" ("c2", "c3", "c4") 
    SELECT "c2", "c3", "c4" 
    FROM "a" 
    RETURNING id
) AS cp;

This is the error I’m getting:

ERROR:  syntax error at or near "INTO"
LINE 4:  INSERT INTO "b" ("c2", "c3", "c4") 
                ^
SQL state: 42601
Character: 46

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

As per the UPDATE syntax https://www.postgresql.org/docs/current/sql-update.html,
from_item should be a table expression. But the "INSERT INTO" can not be a table expression that can be specified in FROM clause.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading