当前位置: 代码迷 >> Sql Server >> case 嵌套正确写法有关问题
  详细解决方案

case 嵌套正确写法有关问题

热度:11   发布时间:2016-04-27 16:22:17.0
case 嵌套正确写法问题
declare   @type   as   int
declare   @id   as   int
set   @type=3
set   @id=1
SELECT      
            CASE  
                  WHEN   @type   in(1,4,6)   THEN  
select  
case  
when   @id   in(1)   then   'A '
when   @id   in(2)   then   'a '
end
                  WHEN   @type   in(2,5)   THEN  
select  
case  
when   @id   in(1)   then   'B '
when   @id   in(2)   then   'b '
end
                  WHEN   @type   in(3)   THEN   'C '
            END


------解决方案--------------------
SELECT
CASE
WHEN @type in(1,4,6) THEN
case
when @id in(1) then 'A '
when @id in(2) then 'a '
end
WHEN @type in(2,5) THEN
case
when @id in(1) then 'B '
when @id in(2) then 'b '
end
WHEN @type in(3) THEN 'C '
END

------解决方案--------------------
declare @type as int
declare @id as int
set @type=3
set @id=1
SELECT
CASE
WHEN @type in(1,4,6) THEN
--select
case
when @id in(1) then 'A '
when @id in(2) then 'a '
end
WHEN @type in(2,5) THEN
--select
case
when @id in(1) then 'B '
when @id in(2) then 'b '
end
WHEN @type in(3) THEN 'C '
END
  相关解决方案