Skip to content

Instantly share code, notes, and snippets.

@omerkaya1
Created November 28, 2022 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omerkaya1/11cafc0e205d71c1652c3ff3b3558b2d to your computer and use it in GitHub Desktop.
Save omerkaya1/11cafc0e205d71c1652c3ff3b3558b2d to your computer and use it in GitHub Desktop.
WITH get AS (
SELECT id FROM table_name WHERE name=$1
), new AS (
INSERT INTO table_name (name) VALUES ($1) ON CONFLICT (name) DO NOTHING RETURNING id
)
SELECT id FROM get
UNION ALL
SELECT id FROM new;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment