Skip to content

Instantly share code, notes, and snippets.

@larrasket
Created December 23, 2022 21:07
Show Gist options
  • Save larrasket/dd42cc5bd865bccec953a4a5cd2583f4 to your computer and use it in GitHub Desktop.
Save larrasket/dd42cc5bd865bccec953a4a5cd2583f4 to your computer and use it in GitHub Desktop.
SQL Server, delete if exists, insert if doesn't exist (aka toggle)
MERGE INTO SIJL.USERS AS target
USING (
SELECT '<username>' AS username, '<email>' AS email
) AS source
ON (target.username = source.username AND target.email = source.email)
WHEN MATCHED THEN
DELETE
WHEN NOT MATCHED THEN
INSERT (hash, first_name, last_name, email, username, age)
VALUES (HASHBYTES('SHA2_256', '<password>'), '<first_name>', '<last_name>', '<email>', '<username>', <age>)
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment