在sql中对case语句DESC进行排序

时间:2013-02-04 15:36:27

标签: sql sorting sql-server-express

我想将我的ArticleID分类为up(DESC)。如何编辑此代码来执行此操作? 非常感谢你

ORDER  BY CASE 
        WHEN @order = 'AddArticleType.ArticleID' THEN CONVERT(nvarchar,AddArticleType.ArticleID) 
        WHEN @order = 'Title' THEN CONVERT(nvarchar, Title) 
        WHEN @order = 'family' THEN CONVERT(nvarchar, family)
        WHEN @order = 'AddArticleType.ArticleType' THEN 
         CONVERT(nvarchar, AddArticleType.ArticleType ) 
      END 

1 个答案:

答案 0 :(得分:2)

回到前一篇文章中@ Gordon的好回答,这应该有效:

ORDER  BY 
   CASE WHEN @order = 'AddArticleType.ArticleID' THEN AddArticleType.ArticleID END DESC,
   CASE WHEN @order = 'Title' THEN Title END,
   CASE WHEN @order = 'family' THEN family END,
   CASE WHEN @order = 'AddArticleType.ArticleType' THEN AddArticleType.ArticleType END

我简化了您的测试代码:

http://sqlfiddle.com/#!3/8a2d3/9

祝你好运。