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

关于SQL case的问题
先上图:
SQL code

ORDER BY CASE when @P_SORTEXPRESSION = 'COURSE_DESC'  THEN COURSE_DESC,T.USER_GRADE,
                                          T.USER_GRADE_LEVEL,
                                          T.DIFFICULTY_LEVEL end,
                         CASE when @P_SORTEXPRESSION = 'COURSE_DESC DESC' THEN COURSE_DESC DESC, T.USER_GRADE,
                                          T.USER_GRADE_LEVEL,
                                          T.DIFFICULTY_LEVEL END,
                         CASE WHEN @P_SORTEXPRESSION = 'USER_GRADE' THEN USER_GRADE, T.COURSE_DESC,
                                          T.USER_GRADE_LEVEL,
                                          T.DIFFICULTY_LEVEL END,
                         CASE WHEN @P_SORTEXPRESSION = 'USER_GRADE DESC' THEN USER_GRADE DESC, T.COURSE_DESC,
                                          T.USER_GRADE_LEVEL,
                                          T.DIFFICULTY_LEVEL END,
                         CASE WHEN @P_SORTEXPRESSION = 'USER_GRADE_LEVEL' THEN USER_GRADE_LEVEL, T.COURSE_DESC,
                                          T.USER_GRADE,
                                          T.DIFFICULTY_LEVEL END,
                         CASE WHEN @P_SORTEXPRESSION = 'USER_GRADE_LEVEL DESC' THEN USER_GRADE_LEVEL DESC, T.COURSE_DESC,
                                          T.USER_GRADE,
                                          T.DIFFICULTY_LEVEL END


相信大家都看懂我想干嘛了,对,排序,但是这个Case Then后面是不是只能跟一个值?那要想实现我想要的这种结果该如何操作?!

------解决方案--------------------
你这个需要使用判断语句,然后去执行.
例如:
if @P_SORTEXPRESSION = 'COURSE_DESC'
select .... from ... order by OURSE_DESC,T.USER_GRADE,T.USER_GRADE_LEVEL,T.DIFFICULTY_LEVEL
else
if @P_SORTEXPRESSION = 'COURSE_DESC DESC'
select .... from ... order by OURSE_DESC DESC, T.USER_GRADE,T.USER_GRADE_LEVEL,T.DIFFICULTY_LEVEL
....