0

I want to create a merge query so that I can insert the new incoming record and update the record if there is any change in record. I created a query as:

MERGE into sales AS TARGET 
USING sales1 AS SOURCE  
ON (TARGET.Order_ID = SOURCE.Order_ID) 
WHEN NOT MATCHED BY TARGET  
THEN INSERT VALUES (select * from SOURCE)

But i am getting a error as ERROR: syntax error at or near "MERGE". My Postgres version is PostgreSQL 11.2 My question is in which version does PostgreSQL support merge query. So does PostgreSQL support merge query or not?

What this link[https://wiki.postgresql.org/wiki/MergeTestExamples] refer to? If this link is correct then how to execute merge query in PostgreSQL?

dharmesh mehta
  • 73
  • 2
  • 11
  • The link is not the manual, but the Wiki where ideas are documented or discussed. And you apparently overlooked the message at the top that says: "**This was never integrated into PostgreSQL, and requires significant work to be production quality**" - If you want to see what commands are supported, check the [SQL reference](https://www.postgresql.org/docs/current/sql-commands.html) - you can use `insert on conflict` instead –  Dec 21 '19 at 07:41
  • So there is no such **merge query** in PostgreSQL its just a concept or idea which needs to be looked on. – dharmesh mehta Dec 21 '19 at 07:47
  • There is no merge statement but you should look at the "Insert ... on confilct" – Belayer Dec 21 '19 at 07:50
  • Okay, I have looked on Insert ... on conflict. One more thing this is [one](https://medium.com/@thomaspt748/how-to-upsert-data-into-relational-database-using-apache-spark-part-2-45a9d49d0f43) article it says using spark call merge query then what it is? – dharmesh mehta Dec 21 '19 at 07:51
  • Your referenced article is about processing Merge in DB2, not Postgres. Different DBMS, different rules. – Belayer Dec 21 '19 at 18:39
  • So ultimately it will not work in PostgreSQL, its something related to Informix database. There it says use merge query, then i searched of merge query in PostgreSQL where i found this wiki link of PostgreSQL. But when ran that merge query error came. – dharmesh mehta Dec 22 '19 at 05:28

0 Answers0