我有以下来自 SQL 语句的片段
ROW_NUMBER() OVER (ORDER BY CASE
WHEN @SortBy = 'column1 ASC' THEN cast(column1 AS sql_variant)
WHEN @SortBy = 'column2 ASC' THEN cast(column2 AS sql_variant)
WHEN @SortBy = 'column3 ASC' THEN cast(column3 AS sql_variant)
WHEN @SortBy = 'column4 ASC' THEN cast(column4 AS sql_variant)
ELSE NULL
END ASC,
CASE
WHEN @SortBy = 'column1 DESC' THEN cast(column1 AS sql_variant)
WHEN @SortBy = 'column2 DESC' THEN cast(column2 AS sql_variant)
WHEN @SortBy = 'column3 DESC' THEN cast(column3 AS sql_variant)
WHEN @SortBy = 'column4 DESC' THEN cast(column4 AS sql_variant)
ELSE NULL
END DESC) AS RowNumber
它有效,但它非常重复,有没有办法使 ASC/DESC 也动态化,所以我不需要重复的CASE
语句?