2016-11-04 37 views
0

我试图更新我的零件表,其中item_class = HW和我无法正确理解。任何人都可以看到我的语法有什么问题。试图用update语句和where子句更新我的数据库表中的零件表

update part 
set unit_price = unit_price + 1.05 
where item_class = ' HW ' ; 

它提供了没有错误,但它不更新正确的行。它说它实际上需要更新4行时将更新0行。

+2

你有两个空间在你的HW左右。是这样吗? – scaisEdge

+0

不是非常感谢你 – Clay

+1

请将你的问题标题改为有意义的东西。你已经重复了标签,这是完全无用的,并且删除它们留下*(更新语句)*,这是无用的。您的标题应该解释您所问的问题或您遇到的问题,这些问题对于将来在搜索结果中看到它的用户会有意义。 –

回答

0

你可以有错误的空间,尝试使用

update part 
set unit_price = unit_price + 1.05 
where item_class like '*HW*' ; 

update part 
set unit_price = unit_price + 1.05 
where item_class like '* HW *' ; 

update part 
set unit_price = unit_price + 1.05 
where item_class = 'HW' ; 
+0

是的,我删除了空间,它像一个魅力。非常感谢你,我觉得把这些空间放进去并永远不会意识到这一点。 – Clay

+0

我用过最后一个。 – Clay

+0

好吧,如果我的回答是正确的,请将其标记为已接受...看到这里如何 http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – scaisEdge

1

不知道你的数据很难说,但我看到几个可能出现的问题。一个是“HW”周围的空间。根据该领域的内容,很难确切地知道哪里的条款是正确的。如果硬件是较长字符组的一部分(人们有时会错误地在逗号字段中存储逗号分隔列表),则可能需要使用LIKE函数。或者可能是删除不需要的空间将解决这个问题。因此,从select语句开始:

Select * from part where item_class = ' HW ' ; 
Select * from part where item_class = 'HW' ; 
Select * from part where item_class like '*HW' ; 
Select * from part where item_class like 'HW*' ; 
Select * from part where item_class like '*HW*' ; 

一旦找到返回所需记录的那个,然后在更新语句中使用它。

别的东西,我看到了可能导致的问题是:

set unit_price = unit_price + 1.05 

如果单价可以为NULL,则需要将其转换成对于和工作0值。否则,NULL加上任何金额= NUll。以下可以工作。

set (IIf([unit_price] Is Null, 0, [unit_price]))+1.05 
+0

是的第二个工程。那是无意的空间。谢谢 – Clay