Talk:SQL MERGE

From PostgreSQL wiki
Jump to navigationJump to search

3/21/14 edit to add Status section

  • On 3/21/14, I added the "Status" section because this is the top result on Google, and no where on the web is it very clear that Postgresql does NOT implement Upsert or MERGE yet. I am a supporter of Postgres, but not having the simple implementation status information easily searchable/available leads to wasting time explaining, wondering, and searching for both existing users and prospective users of the database.
  • If there is another location on the web where this information is prominent, I would like to know! (And add it to this wiki, which appears to have become authoritative on this topic according to Google's ranking algorithm).

Older comment

I looked at the description of the different MERGE syntaxes and it does not include the one I used to use on MSSQL 2008.

 MERGE INTO Event AS t
 USING @EventMerge AS s
 ON ( t.EventCode = s.EventCode)
 WHEN MATCHED AND t.Description <> s.Description THEN
 UPDATE SET t.Description = s.Description
 WHEN NOT MATCHED BY TARGET THEN
 INSERT (EventCode, Description) VALUES (s.EventCode, s.Description)
 WHEN NOT MATCHED BY SOURCE THEN
 DELETE
 ;

I found this useful for setting the entire contents of lookup-tables, and for normalizing table contents for tests with the minimum of writes to disk. Differentiating between cases where something is in the source but not the target, or the target but not the source, strikes me as useful. How would one do that using the proposed syntax?