这个SQL查询有什么问题?

时间:2011-07-22 15:25:49

标签: sql-server

UPDATE UserListColumnData 
set  UserListColumnValue = (case when Contains(ProductDetailObjects.InnerText, **ProductDetailObjects**.AdminInnerText) then 
'Condition True'Else 'Condition False' End)
FROM         VariableDataObjectMaping INNER JOIN  
                      UserListColumnData ON VariableDataObjectMaping.UserListColumnID = UserListColumnData.UserListColumnID INNER JOIN                    
                      UserListColumns ON UserListColumnData.UserListColumnID=UserListColumns.UserListColumnID INNER JOIN                       
                      ProductDetailObjects ON VariableDataObjectMaping.ObjectId = ProductDetailObjects.ObjectID INNER JOIN  
                      ProductDetail ON ProductDetailObjects.ProductDetailID = ProductDetail.ProductDetailID INNER JOIN  
                      Products ON ProductDetail.ProductID = Products.ProductID  
WHERE     (Products.ProductID = 275)

编译后给出以下错误。

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'ProductDetailObjects'.

但是当我在包含函数中写任何字符串时,它就像。

UPDATE UserListColumnData 
set  UserListColumnValue = (case when Contains(ProductDetailObjects.InnerText, 'aab') then 
'Condition True'Else 'Condition False' End)
FROM         VariableDataObjectMaping INNER JOIN  
                      UserListColumnData ON VariableDataObjectMaping.UserListColumnID = UserListColumnData.UserListColumnID INNER JOIN                    
                      UserListColumns ON UserListColumnData.UserListColumnID=UserListColumns.UserListColumnID INNER JOIN                       
                      ProductDetailObjects ON VariableDataObjectMaping.ObjectId = ProductDetailObjects.ObjectID INNER JOIN  
                      ProductDetail ON ProductDetailObjects.ProductDetailID = ProductDetail.ProductDetailID INNER JOIN  
                      Products ON ProductDetail.ProductID = Products.ProductID  
WHERE     (Products.ProductID = 275) 

为什么它不使用字符串列名(ProductDetailObjects.AdminInnerText ????)

4 个答案:

答案 0 :(得分:4)

您只能在CONTAINS中使用常量作为搜索查询。

答案 1 :(得分:2)

在ELSE之前添加空格

'Condition True'Else 

编辑:

像这样更新:

CASE 
      WHEN ProductDetailObjects.InnerText 
      LIKE '%' + ProductDetailObjects.AdminInnerText + '%'       
      THEN  'Condition True'
      ELSE 'Condition False' 
END

答案 2 :(得分:1)

可能是这样的:

'Condition True'Else 'Condition False' End)
                ^--- missing a space here

答案 3 :(得分:1)

您不能将字段名称用作CONTAINS的第二个参数,只能使用文字字符串或变量;它旨在查找第一个参数中列的所有值的特定搜索子句。