日期:2014-05-17  浏览次数:20469 次

一个简单的sql语句
要把一段c#代码转成sql实现
switch(textbox.text)
case '1'
case '2'
case '3'
   string= "the first"
break;
case'4'
   string= "the second"
break;

转成sql后我想这么实现:
set @result = 
case @input
   when '1' then 'the first'
问题是,有很多同样的情况我不想每一种情况都写一遍,也就是:
set @result = 
case @input
   when '1' then 'the first'
   when '2' then 'the first'
....多个then后面都是一样的,我能用类似or的写法,只写一个then.
when '1' or '2' then 'the first'

------解决方案--------------------

DECLARE @text int

SELECT CASE @text WHEN 1 THEN 1 WHEN 2 THEN 2 WHEN 3  THEN 3 ELSE 4 END
SELECT CASE WHEN @text=1  THEN  1  WHEN @text=2 THEN 2 WHEN @text =3 OR @text=4   THEN 3 ELSE 4 END

--case when 在sql里有上面两种写法