Sql server 根据动态排序参数和方向,是否有更简单的方法对此表进行排序?

Sql server 根据动态排序参数和方向,是否有更简单的方法对此表进行排序?,sql-server,tsql,Sql Server,Tsql,我有一个过程,它生成一个表,该表根据提供给它的初始参数进行排序。这是我必须做的事情来分类: ORDER BY (CASE WHEN @SortExpression = 'Description' AND @SortDirection = 'ASC' THEN s.[Description] END) ASC, (CASE WHEN @SortExpression = 'Description' AND @SortDirection = 'DESC' THEN s.[Des

我有一个过程,它生成一个表,该表根据提供给它的初始参数进行排序。这是我必须做的事情来分类:

ORDER BY (CASE WHEN @SortExpression = 'Description' AND @SortDirection = 'ASC' THEN s.[Description] END) ASC,
            (CASE WHEN @SortExpression = 'Description' AND @SortDirection = 'DESC' THEN s.[Description] END) DESC,
            (CASE WHEN @SortExpression = 'SiteId' AND @SortDirection = 'ASC' THEN t.SiteId END) ASC,
            (CASE WHEN @SortExpression = 'SiteId' AND @SortDirection = 'DESC' THEN t.SiteId END) DESC,
            (CASE WHEN @SortExpression = 'CancerConfirmed' AND @SortDirection = 'ASC' THEN t.CancerConfirmed END) ASC,
            (CASE WHEN @SortExpression = 'CancerConfirmed' AND @SortDirection = 'DESC' THEN t.CancerConfirmed END) DESC,
            (CASE WHEN @SortExpression = 'NoCancer' AND @SortDirection = 'ASC' THEN t.NoCancer END) ASC,
            (CASE WHEN @SortExpression = 'NoCancer' AND @SortDirection = 'DESC' THEN t.NoCancer END) DESC,
            (CASE WHEN @SortExpression = 'SuspectCancer' AND @SortDirection = 'ASC' THEN t.SuspectCancer END) ASC,
            (CASE WHEN @SortExpression = 'SuspectCancer' AND @SortDirection = 'DESC' THEN t.SuspectCancer END) DESC,
            (CASE WHEN @SortExpression = 'TotalCancer' AND @SortDirection = 'ASC' THEN t.TotalCancer END) ASC,
            (CASE WHEN @SortExpression = 'TotalCancer' AND @SortDirection = 'DESC' THEN t.TotalCancer END) DESC,
            (CASE WHEN @SortExpression = 'NoFirstAppointment' AND @SortDirection = 'ASC' THEN t.NoFirstAppointment END) ASC,
            (CASE WHEN @SortExpression = 'NoFirstAppointment' AND @SortDirection = 'DESC' THEN t.NoFirstAppointment END) DESC,
            (CASE WHEN @SortExpression = 'NoTreatment' AND @SortDirection = 'ASC' THEN t.NoTreatment END) ASC,
            (CASE WHEN @SortExpression = 'NoTreatment' AND @SortDirection = 'DESC' THEN t.NoTreatment END) DESC,
            (CASE WHEN @SortExpression = 'NoDecisionToTreat' AND @SortDirection = 'ASC' THEN t.NoDecisionToTreat END) ASC,
            (CASE WHEN @SortExpression = 'NoDecisionToTreat' AND @SortDirection = 'DESC' THEN t.NoDecisionToTreat END) DESC,
            (CASE WHEN @SortExpression = 'InterTrustDiscussion' AND @SortDirection = 'ASC' THEN itd.InterTrustDiscussion END) ASC,
            (CASE WHEN @SortExpression = 'InterTrustDiscussion' AND @SortDirection = 'DESC' THEN itd.InterTrustDiscussion END) DESC,
            (CASE WHEN @SortExpression = 'InterTrustTransfer' AND @SortDirection = 'ASC' THEN i.InterTrustTransfer END) ASC,
            (CASE WHEN @SortExpression = 'InterTrustTransfer' AND @SortDirection = 'DESC' THEN i.InterTrustTransfer END) DESC,
            (CASE WHEN @SortExpression = 'WaitingOnInvestigation' AND @SortDirection = 'ASC' THEN w.WaitingOnInvestigation END) ASC,
            (CASE WHEN @SortExpression = 'WaitingOnInvestigation' AND @SortDirection = 'DESC' THEN w.WaitingOnInvestigation END) DESC,
            (CASE WHEN @SortExpression = 'Oncology' AND @SortDirection = 'ASC' THEN o.Oncology END) ASC,
            (CASE WHEN @SortExpression = 'Oncology' AND @SortDirection = 'DESC' THEN o.Oncology END) DESC

我不是SQL专家,所以这是我能想到的最好的。有谁知道有更好或更简洁的方法可以做到这一点?我对此表示怀疑,我只是想问一下。

这是对Dynamc SQL的强烈要求。看吧,但是(然后再看看它的其余部分)。@Jeroenmoster谢谢,这是最好的方法。把这个作为答案。