我有这样的查询,但它不起作用,带有CASE EXPRESSION的IN关键字出了什么问题?
Select State = case c.addressId
when in('2552','2478','2526') then 'IN'
when in ('9999') then 'OUT'
else 'UNKNOWN'
end,
name, time
from x;
我使用SQL Server 2008,这是错误消息:
关键字附近的语法不正确 '中'。
答案 0 :(得分:4)
你的语法错了。它应该是CASE WHEN [COLUMN] in (...)
:
Select
case when c.addressId in('2552','2478','2526') then 'IN'
when c.addressId in ('9999') then 'OUT'
else 'UNKNOWN'
end as State,
name, time
from contact c;
答案 1 :(得分:-1)
考虑使用连接。
SELECT ISNULL(a.[State], 'UNKNOWN') [State]
, c.[Name]
, c.[Time]
FROM [contact] c
LEFT OUTER JOIN
(
SELECT 2552 AddressId, 'IN' [State]
UNION ALL SELECT 2478, 'IN'
UNION ALL SELECT 2526, 'IN'
UNION ALL SELECT 9999, 'OUT'
) [address] a ON a.AddressId = c.AddressId;