Skip to content

Instantly share code, notes, and snippets.

@jamesdavidson
Created July 17, 2023 17:28
Show Gist options
  • Save jamesdavidson/9150d8698aee613a7ef47166c4bd6c9f to your computer and use it in GitHub Desktop.
Save jamesdavidson/9150d8698aee613a7ef47166c4bd6c9f to your computer and use it in GitHub Desktop.
find or create values
-- https://stackoverflow.com/a/70312948
WITH
extant AS (
SELECT id FROM access_code WHERE ("value") = ('asdf')
),
inserted AS (
INSERT INTO access_code ("value")
SELECT 'asdf'
WHERE NOT EXISTS (SELECT FROM extant)
RETURNING id
)
SELECT id FROM inserted
UNION ALL
SELECT id FROM extant
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment