在SQL Server 2008中,是否有一种缩写方法可以将CASE布尔表达式与表达式为真时要选择的值进行匹配?
而不是
CASE
when item='a' then 'Apple'
when item='b' then 'Ball'
when item IN ('c','d') then 'Pet'
when item !='zz' then 'object'
else 'Bad_Item'
我想要类似于以下内容之一的东西。这些当然是伪代码,但这个想法是使关联更紧密,而不是在/然后对时继续编写更多
。这样的东西存在吗?
CASE when item ;(=,'a','Apple') ;(=,'b','Ball') ;(=,'c' or 'd','Pet') ;(!='zz','object') ;'Bad item' END
这样的东西存在吗?
CASE when item ;= ('a','b','c' or 'd' : 'Apple','Ball','Pet','Object') ;!= ('zz' : 'Object') ;'Bad item' END
同样,这些是伪代码,但只是想知道是否有更快或更简单的东西,或者列出所有要检查的内容,然后是要选择的所有值。
没有你描述的速记。您可以使用@sarwar026提到的备用 CASE 语法稍微缩短代码,但他发布的<>
行不起作用。除了相等之外,您不能将该形式用于任何东西 - 没有不等式,没有在 或>=/<= 之间使用的范围,没有 IN((,甚至没有空检查。
组合简单表达式和搜索CASE
表达式的示例如下:
declare @item as VarChar(10) = 'c'
select case @item
when 'a' then 'Apple'
when 'b' then 'Ball'
else case
when @item in ( 'c', 'd' ) then 'Pet'
when @item != 'zz' then 'object'
else 'Bad_Item' end
end
据我所知,以下作品
CASE item
when 'a' then 'Apple'
when 'b' then 'Ball'
when 'c' then 'Pet'
when 'd' then 'Pet'
// the next line is not possible, so please discard the next line
*when <>'zz' then 'object' // not sure about this syntax*
else 'Bad_Item'
END