如何按SQL查询中所选列的#排序

如何按SQL查询中所选列的#排序,sql,sql-server,Sql,Sql Server,您好,我有以下SQL存储过程查询 @colindex int SELECT di.folio, di.name, di.region, di.amount FROM datainfo di WHERE di.isactive = 1 ORDER BY .....@colindex 'where colindex is the index of the column returned 例如: 如果@colindex=1,那么我想要排序的列是“folio”列 如果@col

您好,我有以下SQL存储过程查询

@colindex int

SELECT
   di.folio,
   di.name,
   di.region,
   di.amount
FROM datainfo di
WHERE di.isactive = 1
ORDER BY .....@colindex 'where colindex is the index of the column returned
例如:

如果@colindex=1,那么我想要排序的列是“folio”列

如果@colindex=4,那么我要根据的列是“amount”列

关于如何在SQL中处理这个问题,有什么线索吗


谢谢。

不确定这是否会非常快,但您可以添加:

order by case @colindex when 1 then folio when 2 then ... end
ORDER BY CASE @colindex 
             WHEN 1 THEN [MyColumn1]
             WHEN 2 THEN [MyColumn2]
             WHEN 3 THEN [MyColumn3]
             WHEN 4 THEN [MyColumn4]
         END
要添加asc/desc,请执行以下操作:

ORDER BY CASE @sortorder
    WHEN 'ASC' THEN
        CASE @colindex 
            WHEN 1 THEN [MyColumn1]
            WHEN 2 THEN [MyColumn2]
            WHEN 3 THEN [MyColumn3]
            WHEN 4 THEN [MyColumn4]
         END
    END,
    CASE @sortorder
        WHEN 'DES' THEN
        CASE @colindex 
            WHEN 1 THEN [MyColumn1]
            WHEN 2 THEN [MyColumn2]
            WHEN 3 THEN [MyColumn3]
            WHEN 4 THEN [MyColumn4]
         END
    END DESC

为了解释,这两个顺序都适用,但当
@sortorder
变量为“ASC”时,第一个顺序将持续为
NULL

不确定这是否会非常快,但您可以添加:

ORDER BY CASE @colindex 
             WHEN 1 THEN [MyColumn1]
             WHEN 2 THEN [MyColumn2]
             WHEN 3 THEN [MyColumn3]
             WHEN 4 THEN [MyColumn4]
         END
@colindex int

SELECT
   di.folio,
   di.name,
   di.region,
   di.amount
FROM datainfo di
WHERE di.isactive = 1
ORDER BY case @colindex  when 1 then di.folio when 4 then di.amount end
要添加asc/desc,请执行以下操作:

ORDER BY CASE @sortorder
    WHEN 'ASC' THEN
        CASE @colindex 
            WHEN 1 THEN [MyColumn1]
            WHEN 2 THEN [MyColumn2]
            WHEN 3 THEN [MyColumn3]
            WHEN 4 THEN [MyColumn4]
         END
    END,
    CASE @sortorder
        WHEN 'DES' THEN
        CASE @colindex 
            WHEN 1 THEN [MyColumn1]
            WHEN 2 THEN [MyColumn2]
            WHEN 3 THEN [MyColumn3]
            WHEN 4 THEN [MyColumn4]
         END
    END DESC


为了解释,这两个顺序都适用,但是当
@sortorder
变量为'ASC'时,第一个顺序将持续为
NULL

为什么要被否决?还有三个人在这个家伙之后提出了同样的建议,首先,这里只有两个答案,其次,bwperrin首先回答了这个问题。如果你想了解narky,请直截了当地了解事实。是否也可以使用@ordertype(asc或desc)并将其添加到查询中为什么被否决?还有三个人在这个家伙之后提出了同样的建议,首先,这里只有两个答案,其次,bwperrin首先回答了这个问题。如果你想了解narky,请直截了当地了解事实。是否也可以使用@ordertype(asc或desc)并将其添加到查询中秒比我快,该死的你回车:秒比我快,该死的你回车:PIs也可以有一个@ordertype(asc或desc)并将其添加到查询中?关于如何做的任何线索?非常感谢你的帮助!case@ordertype when'A',Asc when'D',然后Desc end您需要传递给值为'A'或'D'的变量。这不起作用-您必须执行单独的case子句,如在@typ='A'时按大小写顺序,否则为NULL end Asc,或。。。如果条件是数字,当@typ='A'时,您可以按*大小写排序,那么1 else-1 endIs也可以有@ordertype(asc或desc)并将其添加到查询中?关于如何做的任何线索?非常感谢你的帮助!case@ordertype when'A',Asc when'D',然后Desc end您需要传递给值为'A'或'D'的变量。这不起作用-您必须执行单独的case子句,如在@typ='A'时按大小写顺序,否则为NULL end Asc,或。。。如果条件是数字,则可以在@typ='A'时按*大小写排序,然后按1 else-1 end排序
@colindex int

SELECT
   di.folio,
   di.name,
   di.region,
   di.amount
FROM datainfo di
WHERE di.isactive = 1
ORDER BY case @colindex  when 1 then di.folio when 4 then di.amount end