我有一个更新,将列当前整数添加到另一列的整数但如果其他列的整数是null,那么我希望它只是将第一列添加到零。
Dim cmd As New SqlCommand("UPDATE ATable SET BoxQuantity = ATable.BoxQuantity + (Select sum(BTable.BoxQTY) FROM BTable Where PartNumber Like Bolt%'", con)
如果BTable中没有螺栓,那么我希望它只是添加零而不是由于空值而得到错误,我将如何进行此操作?
UPDATE ATable
SET BoxQuantity = ATable.BoxQuantity +
(Select sum(BTable.BoxQTY) FROM BTable Where PartNumber Like Bolt%')
答案 0 :(得分:1)
您可以使用COALESCE()
:
UPDATE ATable
SET BoxQuantity = ATable.BoxQuantity +
(SELECT COALESCE(SUM(BTable.BoxQTY), 0)
FROM BTable WHERE PartNumber LIKE 'Bolt%')