SQL Server中的CASE语句不支持OR

不支持CASE语句的WHEN子句中的OR操作符。我该怎么做呢?

CASE ebv.db_no
WHEN 22978 OR 23218 OR 23219 THEN 'WECS 9500'
ELSE 'WECS 9520'
END as wecs_system
3075834 次浏览

试一试

CASE WHEN ebv.db_no IN (22978,23218,23219) THEN 'WECS 9500' ELSE 'WECS 9520' END
CASE WHEN ebv.db_no IN (22978, 23218, 23219) THEN 'WECS 9500'
ELSE 'WECS 9520'
END as wecs_system

这种格式要求你使用:

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
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

您可以使用WHEN所拥有的其中一个表达式,但不能将两者混合使用。

  1. 当when_expression < p >

    是一个简单表达式,当使用简单CASE格式时,input_expression将与之进行比较。When_expression是任何有效的表达式。input_expression和每个when_expression的数据类型必须相同或必须是隐式转换

  2. 当Boolean_expression < p >

    使用搜索的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)。

SELECT
Store_Name,
CASE Store_Name
WHEN 'Los Angeles' THEN Sales * 2
WHEN 'San Diego' THEN Sales * 1.5
ELSE Sales
END AS "New Sales",
Txn_Date
FROM Store_Information;
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已经有很多答案了。我将解释何时以及如何使用CASE

您可以在SQL查询中的任何地方使用CASE表达式。CASE表达式可以在SELECT语句、WHERE子句、Order by子句、HAVING子句、Insert、UPDATE和DELETE语句中使用。

CASE表达式有以下两种格式:

  1. 简单CASE表达式

    CASE expression
    WHEN expression1 THEN Result1
    WHEN expression2 THEN Result2
    ELSE ResultN
    END
    

    这将一个表达式与一组简单表达式进行比较,以查找结果。该表达式将一个表达式与每个WHEN子句中的表达式进行比较,以获得等价性。如果WHEN子句中的表达式被匹配,则返回THEN子句中的表达式。

    这就是上级的问题所在。22978 OR 23218 OR 23219将不会得到一个等于表达式的值,即ebv.db_no。这就是为什么它会报错。input_expression和每个when_expression的数据类型必须相同或必须是隐式转换

  2. 搜索CASE表达式

    CASE
    WHEN Boolean_expression1 THEN Result1
    WHEN Boolean_expression2 THEN Result2
    ELSE ResultN
    END
    

    此表达式计算一组布尔表达式以查找结果。该表达式允许在每个布尔表达式中使用比较运算符和逻辑运算符and /OR

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)

希望这个用例将来能帮助到别人。

# EYZ0

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
select id,phno,case gender
when 'G' then 'M'
when 'L' then 'F'
else
'No gender'
end
as gender
from contacts
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