Last active
August 29, 2015 14:27
-
-
Save jueyang/7fc8dbe4dfcde3a22956 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH | |
-- write the new values | |
new(the_geom,description,name) AS ( | |
VALUES (ST_SetSRID(ST_Point(12,12),4326),'an update',123456), | |
(ST_SetSRID(ST_Point(13,13),4326),'an insert',100000) | |
), | |
-- update existing rows | |
upsert AS ( | |
UPDATE {table} original | |
SET the_geom=new.the_geom, description=new.description | |
FROM new WHERE original.name = new.name | |
RETURNING original.name | |
) | |
-- insert missing rows | |
INSERT INTO {table} (the_geom,description,name) | |
SELECT new.the_geom, new.description, new.name FROM new | |
WHERE new.name NOT IN ( | |
SELECT name FROM upsert | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment