I have a feeling I’ve spent too much time on this problem and have been blinded… and hopefully a pair of fresh eyes can help point out a simple error!
This is the MERGE statement I think should work:
Not working:
MERGE assignment_tbl AS target
USING (SELECT 1 s) AS source
ON target.id = @id
AND target.id1 = @id1
AND target.id2 = @id2
WHEN matched AND NOT (target.iFeeScope = @iFeeScope OR target.nFeeAmount = @nFeeAmount) AND (target.bActive = 1) THEN
UPDATE SET target.dLastUpdated = @dNow,
target.dDisabled = @dNow,
target.bActive = 0;
The problem is in my WHEN matched AND NOT statement: (target.iFeeScope = @iFeeScope OR target.nFeeAmount = @nFeeAmount), and the only way around this I’ve found is to break the statement up into two separate (and almost identical) blocks:
Working (but inefficient):
MERGE assignment_tbl AS target
USING (SELECT 1 s) AS source
ON target.id = @id
AND target.id1 = @id1
AND target.id2 = @id2
WHEN matched AND NOT (target.iFeeScope = @iFeeScope) AND (target.bActive = 1) THEN
UPDATE SET target.dLastUpdated = @dNow,
target.dDisabled = @dNow,
target.bActive = 0;
MERGE assignment_tbl AS target
USING (SELECT 1 s) AS source
ON target.id = @id
AND target.id1 = @id1
AND target.id2 = @id2
WHEN matched AND NOT (target.nFeeAmount = @nFeeAmount) AND (target.bActive = 1) THEN
UPDATE SET target.dLastUpdated = @dNow,
target.dDisabled = @dNow,
target.bActive = 0;
What do I need to change in my original statement to achieve the result of the following two statements?
Thank you all in advance!
Solved it on my own… figured it was an issue with the AND/NOT logic!