Skip to content

Instantly share code, notes, and snippets.

@slavama
Created November 12, 2019 15:23
Show Gist options
  • Save slavama/993e2ef9c1d7293e220e8c42d8b7d954 to your computer and use it in GitHub Desktop.
Save slavama/993e2ef9c1d7293e220e8c42d8b7d954 to your computer and use it in GitHub Desktop.
postgresql bulk update
CREATE TABLE billing.mytable(id integer, name text);
INSERT INTO billing.mytable VALUES(1, '1');
INSERT INTO billing.mytable VALUES(2, '2');
INSERT INTO billing.mytable VALUES(3, '3');
UPDATE billing.mytable
SET name = Q.name
FROM (
SELECT (value->>0)::integer AS id, (value->>1)::decimal AS name
FROM json_array_elements('[[1, "100"], [2, "200"]]')
) Q
WHERE mytable.id = Q.id;
SELECT * FROM billing.mytable;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment