How can I replace the merge query with some other code in SQL Server 2012 in below code to get the same functionality.
MERGE ReprocessPRESs with (ROWLOCK) AS TARGET
USING
(
SELECT @PRId AS PRId,
Val AS ESId,
GETDATE() as CreateDate
FROM @ESIds
) AS SOURCE (PRId,ESId,CreateDate)
ON TARGET.PRId= SOURCE.PRId AND TARGET.ESId = SOURCE.ESId
WHEN NOT MATCHED BY TARGET
AND SOURCE.PRId = @PRId
THEN INSERT (PRId,ESId,CreateDate)
VALUES (PRId,ESId,CreateDate)
WHEN NOT MATCHED BY SOURCE
AND TARGET.PRId = @PRId
THEN DELETE;
Where @ESIds is a User defined type of IntList
Basically it should update, insert or delete accordingly.
1条答案
按热度按时间fnatzsnv1#
The exact same code as separate statements would be:
Having said that, you would be advised to use
UPDLOCK, HOLDLOCK
hints on the inner side of theEXISTS
in theINSERT
section.