语法
简单 CASE 函数:
CASE input_expression
WHEN when_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
END
CASE 搜索函数:
CASE
WHEN Boolean_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
END
举例:
--创建表
create table t
(
[name] varchar(50),
finance decimal(18,4)
)
go
--测试数据
insert into t values ('联合银行',89.56)
insert into t values ('泰和银行',30.59)
go
--应用一
select [name] as 名称,finance as 财务,状况=case
when finance>=50 then '良好'
when finance<50 then '一般'
end
from t
go
--应用二
update t set finance=case [name]
when '联合银行' then 78.25
when '泰和银行' then 56.31
end
from t