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

时间:2009-04-02 13:44:22

标签: sql sql-server

我在存储过程中有以下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

4 个答案:

答案 0 :(得分: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 :(得分:7)

您可以在没有动态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

答案 2 :(得分:6)

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

看看here

答案 3 :(得分:1)

这个怎么样:

CASE WHEN @order_by = @order_by  THEN @order_by END

然后没有理由有多个案例。