日期:2014-05-18  浏览次数:20675 次

sql server 有没有条件分支语句?
比如像c语言一样的  
switch(){
case     :

case     :

}
这样的?

------解决方案--------------------
case 列 when 值1 then ... when ... then ... else ... end

case when 条件1 then ... when ... then ... end
------解决方案--------------------
USE pubs
GO
SELECT Category =
CASE type
WHEN 'popular_comp ' THEN 'Popular Computing '
WHEN 'mod_cook ' THEN 'Modern Cooking '
WHEN 'business ' THEN 'Business '
WHEN 'psychology ' THEN 'Psychology '
WHEN 'trad_cook ' THEN 'Traditional Cooking '
ELSE 'Not yet categorized '
END,
CAST(title AS varchar(25)) AS 'Shortened Title ',
price AS Price
FROM titles
WHERE price IS NOT NULL
ORDER BY type, price
COMPUTE AVG(price) BY type
GO