0

我有一种感觉,我在这个问题上花了太多时间并且被蒙蔽了......希望一双新鲜的眼睛可以帮助指出一个简单的错误!

这是我认为应该起作用的MERGE陈述:

不工作:

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;

问题出在我的WHEN matched AND NOT声明中:(target.iFeeScope = @iFeeScope OR target.nFeeAmount = @nFeeAmount),我发现解决这个问题的唯一方法是将声明分成两个单独的(并且几乎相同的)块:

工作(但效率低下):

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; 

我需要在我的原始语句中进行哪些更改才能达到以下两个语句的结果?

谢谢大家!

4

2 回答 2

0

关于什么:

WHEN matched AND target.iFeeScope != @iFeeScope AND target.nFeeAmount != @nFeeAmount AND (target.bActive = 1) THEN
于 2012-08-03T01:29:13.057 回答
0

我自己解决了...认为这是 AND/NOT 逻辑的问题!

MERGE assignment_tbl AS target
USING (SELECT 1 s) AS source
ON target.id = @id
   AND target.id1 = @id1
   AND target.id2 = @id2
   AND target.bActive = 1
WHEN matched AND (NOT (target.iFeeScope = @iFeeScope)) 
             OR (NOT (target.nFeeAmount = @nFeeAmount)) THEN
  UPDATE SET target.dLastUpdated = @dNow,
             target.dDisabled = @dNow,
             target.bActive = 0; 
于 2012-08-03T01:56:08.300 回答