首页 > 解决方案 > SQL更新语句不更新,执行时无错误

问题描述

我每天安排以下两个更新语句来更新列(x_shippit_volumetric_weight),以较大者为准 - 净重或体积重量。 如果它已经存在并且正确,我在那里有 AND 语句来阻止它覆盖任何现有数据。

问题是,即使 x_shippit_volumetric_weight = 0,数据也没有更新?执行这两个更新语句时也没有显示错误。

任何关于它为什么不更新数据的帮助将不胜感激。

UPDATE stock_items 
SET    x_shippit_volumetric_weight = Round( ( stock_items.x_height * stock_items.x_width_cm * stock_items.x_length_cm ) / 4000, 4) 
WHERE Round( ( stock_items.x_height * stock_items.x_width_cm * stock_items.x_length_cm ) / 4000, 4) > stock_items.weight
AND x_shippit_volumetric_weight <> Round( ( stock_items.x_height * stock_items.x_width_cm * stock_items.x_length_cm ) / 4000, 4) 
AND (stock_items.x_height * stock_items.x_width_cm * stock_items.x_length_cm) <> 0; 


UPDATE stock_items 
SET    stock_items.x_shippit_volumetric_weight = Round(( stock_items.weight ), 4) 
WHERE  stock_items.weight > Round( ( stock_items.x_height * stock_items.x_width_cm * stock_items.x_length_cm ) / 4000, 4) 
AND stock_items.x_shippit_volumetric_weight <> Round(( stock_items.weight ), 4); 

标签: sqlsql-update

解决方案


好的,我发现了数据不更新的原因(在某些情况下)。只要 x_shippit_volumetric_weight 具有 NULL 值,它就不会更新。我实际上不明白为什么,但是在其他两个 UPDATE 语句之前添加以下 UPDATE 语句可以缓解这个问题。

UPDATE stock_items 
SET    x_shippit_volumetric_weight = 0 
WHERE  x_shippit_volumetric_weight IS NULL;

推荐阅读