SQL Update conflict with Check constraint 错误
SQL Update conflict with Check constraint error
我正在尝试更新存储过程中的一些数值
SET SubTotal = SubTotal + @ExtCost
, GST = SubTotal * 0.05
, Total = SubTotal + GST
WHERE JobNumber = @JobNumber
@ExtCost
和 @JobNumber
都有效并且工作正常。
我收到这个错误:
The UPDATE statement conflicted with the CHECK constraint "ck_SubTotalandTotal"
检查约束是Total > SubTotal
逻辑对我来说似乎很好,但我不知道为什么它不起作用:(
取如下值的记录为例:
- 小计:10
- 消费税:0.5
- 总计:10.5
现在您执行更新时 @extCost 为 2
SET
SubTotal = SubTotal + @ExtCost,
GST = SubTotal * 0.05,
Total = SubTotal + GST
WHERE JobNumber = @JobNumber
在更新过程中,使用当前值来填充列值,所以这与
相同
SET
SubTotal = 10 + 2,
GST = 10 * 0.05,
Total = 10 + 0.05
WHERE JobNumber = @JobNumber
因此(小计)12 >(总计)10.05 导致约束问题。
您想要的是将计算值考虑在内,因此您需要按字段进行计算。
SET
SubTotal = SubTotal + @ExtCost,
GST = (SubTotal + @ExtCost) * 0.05,
Total = (SubTotal + @ExtCost) + ((SubTotal + @ExtCost) * 0.05)
WHERE JobNumber = @JobNumber
或更短
SET
SubTotal = SubTotal + @ExtCost,
GST = (SubTotal + @ExtCost) * 0.05,
Total = (SubTotal + @ExtCost) * 1.05
WHERE JobNumber = @JobNumber
我正在尝试更新存储过程中的一些数值
SET SubTotal = SubTotal + @ExtCost
, GST = SubTotal * 0.05
, Total = SubTotal + GST
WHERE JobNumber = @JobNumber
@ExtCost
和 @JobNumber
都有效并且工作正常。
我收到这个错误:
The UPDATE statement conflicted with the CHECK constraint "ck_SubTotalandTotal"
检查约束是Total > SubTotal
逻辑对我来说似乎很好,但我不知道为什么它不起作用:(
取如下值的记录为例:
- 小计:10
- 消费税:0.5
- 总计:10.5
现在您执行更新时 @extCost 为 2
SET
SubTotal = SubTotal + @ExtCost,
GST = SubTotal * 0.05,
Total = SubTotal + GST
WHERE JobNumber = @JobNumber
在更新过程中,使用当前值来填充列值,所以这与
相同SET
SubTotal = 10 + 2,
GST = 10 * 0.05,
Total = 10 + 0.05
WHERE JobNumber = @JobNumber
因此(小计)12 >(总计)10.05 导致约束问题。
您想要的是将计算值考虑在内,因此您需要按字段进行计算。
SET
SubTotal = SubTotal + @ExtCost,
GST = (SubTotal + @ExtCost) * 0.05,
Total = (SubTotal + @ExtCost) + ((SubTotal + @ExtCost) * 0.05)
WHERE JobNumber = @JobNumber
或更短
SET
SubTotal = SubTotal + @ExtCost,
GST = (SubTotal + @ExtCost) * 0.05,
Total = (SubTotal + @ExtCost) * 1.05
WHERE JobNumber = @JobNumber