0

我有一个这样的查询,但它不起作用,用 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,这是错误消息:

关键字“in”附近的语法不正确。

4

2 回答 2

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;
于 2011-02-06T16:25:55.123 回答
-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;
于 2011-02-06T17:36:41.920 回答