Last active
December 14, 2015 22:29
-
-
Save christierney402/5158582 to your computer and use it in GitHub Desktop.
TSQL: Return Identity ID using complex UPDATE / INSERT statements #snippet
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
DECLARE @T TABLE ( | |
cartID INT NOT NULL | |
) | |
UPDATE | |
UserCart | |
SET | |
dateModified = GETDATE(), | |
isGift = 1 | |
OUTPUT | |
INSERTED.cartID INTO @T | |
WHERE | |
userID = 1 | |
IF @@ROWCOUNT = 0 | |
INSERT | |
INTO UserCart ( userID, isGift) | |
OUTPUT | |
INSERTED.cartID INTO @T | |
VALUES ( | |
1, | |
1 | |
) | |
SELECT cartID FROM @T |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment