如果可能的输出为零,则不要更新

时间:2016-07-14 04:16:57

标签: mysql vb.net

我有两张桌子,如下所示:

Tables

左表填充了基于列SRNumber的数据,之后您可以看到表stockrequisition和表generalinventory在列{{1}中具有相同的数据}和ItemCode(左表Qty

mySQL命令

RequestedQty

并且该命令位于代码工作的按钮 Update

代码输出

UPDATE GeneralInventory AS tb1 INNER JOIN StockRequisition AS tb2 ON (tb1.ItemCode= tb2.ItemCode) SET tb1.Qty = Format(tb1.Qty - tb2.RequestedQty,2) WHERE tb2.SRNumber = 'SR487SHL' = GeneralInventory.Qty - GeneralInventory.Qty其中StockRequisition.RequestedQty = StockRequisition.SRNumberSR487SHL = GeneralInventory.ItemCode

问题

如何确定StockRequisition.ItemCode的输出是否为负值,以便我可以停止更新命令。像这样的东西

GeneralInventory.Qty

这是我在VB.Net中的完整代码

    if the future output of GeneralInventory.Qty < 0 or Negative then
        Msg("Unable to update your request because some of Items Qty will result to Negative,Please review it first")
else
'Do the Update Command
End If

我希望你能得到我,TYSM将来的帮助。

2 个答案:

答案 0 :(得分:0)

在我看来,你需要的只是一个额外的WHERE子句

格式(tb1.Qty - tb2.RequestedQty,2)&gt; = 0

答案 1 :(得分:0)

只需使用CASE WHEN

UPDATE GeneralInventory AS tb1 
INNER JOIN StockRequisition AS tb2  
ON (tb1.ItemCode= tb2.ItemCode) 
SET tb1.Qty = CASE WHEN tb1.Qty - tb2.RequestedQty < 0 THEN tb1.Qty THEN Format(tb1.Qty - tb2.RequestedQty,2) END
WHERE tb2.SRNumber = 'SR487SHL'

或者您可以使用IF

UPDATE GeneralInventory AS tb1 
INNER JOIN StockRequisition AS tb2  
ON (tb1.ItemCode= tb2.ItemCode) 
SET tb1.Qty = IF(tb1.Qty - tb2.RequestedQty < 0, tb1.Qty, Format(tb1.Qty - tb2.RequestedQty,2))
WHERE tb2.SRNumber = 'SR487SHL'

或者你可以这样做:

UPDATE GeneralInventory AS tb1 
INNER JOIN StockRequisition AS tb2  
ON (tb1.ItemCode= tb2.ItemCode) 
SET tb1.Qty = Format(tb1.Qty - tb2.RequestedQty,2)
WHERE tb2.SRNumber = 'SR487SHL'
AND tb1.Qty >= tb2.RequestedQty

如果你想要返回消息,那么你必须创建一个程序。