Created
September 5, 2013 19:38
-
-
Save davecowart/6455054 to your computer and use it in GitHub Desktop.
SQL Merge Statement
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
select * from toolkitnotifications; | |
MERGE toolkitnotifications as target | |
USING ( | |
select | |
ti.N2ItemId, ti.Id as ToolkitItemId, t.UserId, GETDATE() as Created, 'UpdatedContent' as [Action] | |
FROM ToolkitItems ti | |
LEFT JOIN Toolkits t | |
ON ti.ToolkitId = t.Id | |
WHERE ti.IsDeleted = 0 | |
AND t.IsDeleted = 0 | |
AND t.IsDefault = 0 | |
AND ti.N2ItemId = 196 | |
) as source | |
ON target.N2ItemId = source.N2ItemId | |
AND target.ToolkitItemId = source.ToolkitItemId | |
AND target.UserId = source.UserId | |
AND target.IsRead = 0 | |
AND target.[Action] = source.[Action] | |
WHEN MATCHED THEN | |
UPDATE SET Created = GetDate() | |
WHEN NOT MATCHED THEN | |
INSERT(n2itemid, toolkititemid, userid, Created, [Action]) | |
VALUES (source.N2ItemId, source.ToolkitItemId, source.UserId, GetDate(), source.[Action]); | |
select * from toolkitnotifications; | |
update toolkitnotifications set isread = 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment