Can I store SQL Server sort order in a variable?

前端 未结 4 1097
轻奢々
轻奢々 2020-12-16 06:51

I have the following SQL within a stored procedure. Is there a way to remove the IF statement and pass the \'ASC\'/\'DESC\' option as a variable?

I know I could do t

4条回答
  •  無奈伤痛
    2020-12-16 07:19

    pass in @OrderBy int, where positive is ASC, negative is DESC, actual number is the column to sort by

    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
    

    just make sure you build string that sort properly, notice I used 'YYYY-MM-DD hh:mm:ss.mmm' for the dates and put the numbers into strings. We usually put multiple columns together, so if you sort by surname, forename is used too, etc. Watch out, if you do combine multiple columns you'll need to pad with zeros or spaces.

    If you don't want the SortByAsc and SortByDesc columns to be in the result set, wrap the entire thing in a derived table.

提交回复
热议问题