当前位置:  开发笔记 > 数据库 > 正文

我可以在变量中存储SQL Server排序顺序吗?

如何解决《我可以在变量中存储SQLServer排序顺序吗?》经验,为你挑选了3个好方法。

我在存储过程中有以下SQL.有没有办法删除IF语句并将'ASC'/'DESC'选项作为变量传递?

我知道我可以通过多种不同的方式进行查询,或者返回一个表并在外部对其进行排序等等.我只想知道是否可以避免重复CASE语句.

IF @sortOrder = 'Desc'
  BEGIN
    SELECT * FROM #t_results
    ORDER BY
    CASE WHEN @OrderBy = 'surname'           THEN surname END DESC,
    CASE WHEN @OrderBy = 'forename'          THEN forename END DESC,
    CASE WHEN @OrderBy = 'fullName'          THEN fullName END DESC,
    CASE WHEN @OrderBy = 'userId'            THEN userId END DESC,
    CASE WHEN @OrderBy = 'MobileNumber'      THEN MSISDN END DESC,
    CASE WHEN @OrderBy = 'DeviceStatus'      THEN DeviceStatus END DESC,
    CASE WHEN @OrderBy = 'LastPosition'      THEN LastPosition END DESC,
    CASE WHEN @OrderBy = 'LastAlert'         THEN LastAlert END DESC,
    CASE WHEN @OrderBy = 'LastCommunication' THEN LastCommunication END DESC,
    CASE WHEN @OrderBy = 'LastPreAlert'      THEN LastPreAlert END DESC 
    END
 ELSE
   BEGIN
    SELECT * FROM #t_results
    ORDER BY
    CASE WHEN @OrderBy = 'surname'           THEN surname END DESC,
    CASE WHEN @OrderBy = 'forename'          THEN forename END DESC,
    CASE WHEN @OrderBy = 'fullName'          THEN fullName END DESC,
    CASE WHEN @OrderBy = 'userId'            THEN userId END DESC,
    CASE WHEN @OrderBy = 'MobileNumber'      THEN MSISDN END DESC,
    CASE WHEN @OrderBy = 'DeviceStatus'      THEN DeviceStatus END DESC,
    CASE WHEN @OrderBy = 'LastPosition'      THEN LastPosition END DESC,
    CASE WHEN @OrderBy = 'LastAlert'         THEN LastAlert END DESC,
    CASE WHEN @OrderBy = 'LastCommunication' THEN LastCommunication END DESC,
    CASE WHEN @OrderBy = 'LastPreAlert'      THEN LastPreAlert END DESC 
    END
END

KM... 12

传入@OrderBy int,其中positive是ASC,negative是DESC,实际number是要排序的列

SELECT
    dt.yourColumn1
        ,dt.yourColumn2
        ,dt.yourColumn3
        ,CASE 
            WHEN @OrderBy>0 THEN dt.SortBy
            ELSE NULL
         END AS SortByAsc
        ,CASE 
            WHEN @OrderBy<0 THEN dt.SortBy
            ELSE NULL
         END AS SortByDesc
    FROM (SELECT
              yourColumn1
                  ,yourColumn2
                  ,yourColumn3
                  ,CASE
                      WHEN ABS(@OrderBy) = 1 THEN surname
                      WHEN ABS(@OrderBy) = 2 THEN forename
                      WHEN ABS(@OrderBy) = 3 THEN fullName
                      WHEN ABS(@OrderBy) = 4 THEN CONVERT(varchar(10),userId)
                      WHEN ABS(@OrderBy) = 5 THEN CONVERT(varchar(10),MobileNumber
                      WHEN ABS(@OrderBy) = 6 THEN DeviceStatus
                      WHEN ABS(@OrderBy) = 7 THEN LastPosition
                      WHEN ABS(@OrderBy) = 8 THEN CONVERT(varchar(23),LastAlert,121)
                      WHEN ABS(@OrderBy) = 9 THEN CONVERT(varchar(23),LastCommunication,121)
                      WHEN ABS(@OrderBy) =10 THEN CONVERT(varchar(23),LastPreAlert,121)
                      ELSE NULL
                  END AS SortBy
              FROM YourTablesHere
              WHERE X=Y
         ) dt
    ORDER BY SortByAsc ASC, SortByDesc DESC

只要确保你构建正确排序的字符串,注意我使用了'YYYY-MM-DD hh:mm:ss.mmm'作为日期并将数字放入字符串中.我们通常将多个列放在一起,因此如果您按姓氏排序,也会使用forename等.请注意,如果您组合了多个列,则需要使用零或空格填充.

如果您不希望SortByAsc和SortByDesc列位于结果集中,请将整个事物包装在派生表中.



1> KM...:

传入@OrderBy int,其中positive是ASC,negative是DESC,实际number是要排序的列

SELECT
    dt.yourColumn1
        ,dt.yourColumn2
        ,dt.yourColumn3
        ,CASE 
            WHEN @OrderBy>0 THEN dt.SortBy
            ELSE NULL
         END AS SortByAsc
        ,CASE 
            WHEN @OrderBy<0 THEN dt.SortBy
            ELSE NULL
         END AS SortByDesc
    FROM (SELECT
              yourColumn1
                  ,yourColumn2
                  ,yourColumn3
                  ,CASE
                      WHEN ABS(@OrderBy) = 1 THEN surname
                      WHEN ABS(@OrderBy) = 2 THEN forename
                      WHEN ABS(@OrderBy) = 3 THEN fullName
                      WHEN ABS(@OrderBy) = 4 THEN CONVERT(varchar(10),userId)
                      WHEN ABS(@OrderBy) = 5 THEN CONVERT(varchar(10),MobileNumber
                      WHEN ABS(@OrderBy) = 6 THEN DeviceStatus
                      WHEN ABS(@OrderBy) = 7 THEN LastPosition
                      WHEN ABS(@OrderBy) = 8 THEN CONVERT(varchar(23),LastAlert,121)
                      WHEN ABS(@OrderBy) = 9 THEN CONVERT(varchar(23),LastCommunication,121)
                      WHEN ABS(@OrderBy) =10 THEN CONVERT(varchar(23),LastPreAlert,121)
                      ELSE NULL
                  END AS SortBy
              FROM YourTablesHere
              WHERE X=Y
         ) dt
    ORDER BY SortByAsc ASC, SortByDesc DESC

只要确保你构建正确排序的字符串,注意我使用了'YYYY-MM-DD hh:mm:ss.mmm'作为日期并将数字放入字符串中.我们通常将多个列放在一起,因此如果您按姓氏排序,也会使用forename等.请注意,如果您组合了多个列,则需要使用零或空格填充.

如果您不希望SortByAsc和SortByDesc列位于结果集中,请将整个事物包装在派生表中.



2> Tom H..:

没有动态SQL就可以做到......

SELECT
     *
FROM
     My_Table
WHERE
     Whatever = @something
ORDER BY
     CASE @sort_order
          WHEN 'ASC' THEN
               CASE @order_by
                    WHEN 'surname' THEN surname
                    WHEN 'forename' THEN forename
                    WHEN 'fullname' THEN fullname
                    ELSE surname
               END
          ELSE '1'
     END ASC,
     CASE @sort_order
          WHEN 'DESC' THEN
               CASE @order_by
                    WHEN 'surname' THEN surname
                    WHEN 'forename' THEN forename
                    WHEN 'fullname' THEN fullname
                    ELSE surname
               END
          ELSE '1'
     END DESC



3> Fabio Vinici..:

是的,但您必须使用动态查询.

看看这里.

推荐阅读
mobiledu2402851323
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有