不支持CASE语句的WHEN子句中的OR运算符。我该怎么做呢?
CASE ebv.db_no
WHEN 22978 OR 23218 OR 23219 THEN 'WECS 9500'
ELSE 'WECS 9520'
END as wecs_system
不支持CASE语句的WHEN子句中的OR运算符。我该怎么做呢?
CASE ebv.db_no
WHEN 22978 OR 23218 OR 23219 THEN 'WECS 9500'
ELSE 'WECS 9520'
END as wecs_system
当前回答
关于CASE,已经有很多答案了。我将解释何时以及如何使用CASE。
您可以在SQL查询中的任何地方使用CASE表达式。CASE表达式可以在SELECT语句、WHERE子句、Order by子句、HAVING子句、Insert、UPDATE和DELETE语句中使用。
CASE表达式有以下两种格式:
Simple CASE expression CASE expression WHEN expression1 THEN Result1 WHEN expression2 THEN Result2 ELSE ResultN END This compares an expression to a set of simple expressions to find the result. This expression compares an expression to the expression in each WHEN clause for equivalency. If the expression within the WHEN clause is matched, the expression in the THEN clause will be returned. This is where the OP's question is falling. 22978 OR 23218 OR 23219 will not get a value equal to the expression i.e. ebv.db_no. That's why it is giving an error. The data types of input_expression and each when_expression must be the same or must be an implicit conversion. Searched CASE expressions CASE WHEN Boolean_expression1 THEN Result1 WHEN Boolean_expression2 THEN Result2 ELSE ResultN END This expression evaluates a set of boolean expressions to find the result. This expression allows comparison operators, and logical operators AND/OR with in each Boolean expression.
1.带有CASE表达式的SELECT语句
--Simple CASE expression:
SELECT FirstName, State=(CASE StateCode
WHEN 'MP' THEN 'Madhya Pradesh'
WHEN 'UP' THEN 'Uttar Pradesh'
WHEN 'DL' THEN 'Delhi'
ELSE NULL
END), PayRate
FROM dbo.Customer
-- Searched CASE expression:
SELECT FirstName,State=(CASE
WHEN StateCode = 'MP' THEN 'Madhya Pradesh'
WHEN StateCode = 'UP' THEN 'Uttar Pradesh'
WHEN StateCode = 'DL' THEN 'Delhi'
ELSE NULL
END), PayRate
FROM dbo.Customer
2.用CASE表达式更新语句
-- Simple CASE expression:
UPDATE Customer
SET StateCode = CASE StateCode
WHEN 'MP' THEN 'Madhya Pradesh'
WHEN 'UP' THEN 'Uttar Pradesh'
WHEN 'DL' THEN 'Delhi'
ELSE NULL
END
-- Simple CASE expression:
UPDATE Customer
SET StateCode = CASE
WHEN StateCode = 'MP' THEN 'Madhya Pradesh'
WHEN StateCode = 'UP' THEN 'Uttar Pradesh'
WHEN StateCode = 'DL' THEN 'Delhi'
ELSE NULL
END
3.带有CASE表达式的ORDER BY子句
-- Simple CASE expression:
SELECT * FROM dbo.Customer
ORDER BY
CASE Gender WHEN 'M' THEN FirstName END Desc,
CASE Gender WHEN 'F' THEN LastName END ASC
-- Searched CASE expression:
SELECT * FROM dbo.Customer
ORDER BY
CASE WHEN Gender='M' THEN FirstName END Desc,
CASE WHEN Gender='F' THEN LastName END ASC
4.带有CASE表达的从句
-- Simple CASE expression:
SELECT FirstName ,StateCode,Gender, Total=MAX(PayRate)
FROM dbo.Customer
GROUP BY StateCode,Gender,FirstName
HAVING (MAX(CASE Gender WHEN 'M'
THEN PayRate
ELSE NULL END) > 180.00
OR MAX(CASE Gender WHEN 'F'
THEN PayRate
ELSE NULL END) > 170.00)
-- Searched CASE expression:
SELECT FirstName ,StateCode,Gender, Total=MAX(PayRate)
FROM dbo.Customer
GROUP BY StateCode,Gender,FirstName
HAVING (MAX(CASE WHEN Gender = 'M'
THEN PayRate
ELSE NULL END) > 180.00
OR MAX(CASE WHEN Gender = 'F'
THEN PayRate
ELSE NULL END) > 170.00)
希望这个用例将来能帮助到别人。
源
其他回答
这种格式要求你使用:
CASE ebv.db_no
WHEN 22978 THEN 'WECS 9500'
WHEN 23218 THEN 'WECS 9500'
WHEN 23219 THEN 'WECS 9500'
ELSE 'WECS 9520'
END as wecs_system
否则,使用:
CASE
WHEN ebv.db_no IN (22978, 23218, 23219) THEN 'WECS 9500'
ELSE 'WECS 9520'
END as wecs_system
Select s.stock_code,s.stock_desc,s.stock_desc_ar,
mc.category_name,s.sel_price,
case when s.allow_discount=0 then 'Non Promotional Item' else 'Prmotional
item' end 'Promotion'
From tbl_stock s inner join tbl_stock_category c on s.stock_id=c.stock_id
inner join tbl_category mc on c.category_id=mc.category_id
where mc.category_id=2 and s.isSerialBased=0
您可以使用WHEN所拥有的其中一个表达式,但不能将两者混合使用。
当when_expression 是一个简单表达式,当使用简单CASE格式时,input_expression将与之进行比较。When_expression是任何有效的表达式。input_expression和每个when_expression的数据类型必须相同或必须是隐式转换。 当Boolean_expression 使用搜索的CASE格式计算布尔表达式。Boolean_expression是任何有效的布尔表达式。
你可以编程:
1.
CASE ProductLine
WHEN 'R' THEN 'Road'
WHEN 'M' THEN 'Mountain'
WHEN 'T' THEN 'Touring'
WHEN 'S' THEN 'Other sale items'
ELSE 'Not for sale'
2.
CASE
WHEN ListPrice = 0 THEN 'Mfg item - not for resale'
WHEN ListPrice < 50 THEN 'Under $50'
WHEN ListPrice >= 50 and ListPrice < 250 THEN 'Under $250'
WHEN ListPrice >= 250 and ListPrice < 1000 THEN 'Under $1000'
ELSE 'Over $1000'
END
但在任何情况下,你都可以预期变量ranking将在布尔表达式中进行比较。
参见CASE (Transact-SQL) (MSDN)。
UPDATE table_name
SET column_name=CASE
WHEN column_name in ('value1', 'value2',.....)
THEN 'update_value'
WHEN column_name in ('value1', 'value2',.....)
THEN 'update_value'
END
table_name =需要操作的表名。
column_name =需要设置的列名/字段名。
update_value =需要设置的column_name的值
CASE
WHEN ebv.db_no = 22978 OR
ebv.db_no = 23218 OR
ebv.db_no = 23219
THEN 'WECS 9500'
ELSE 'WECS 9520'
END as wecs_system