我了解我可以按如下顺序在sql查询中按顺序使用变量:
order by case when @var1 = 'priority' then priority end desc, case when @var2 = 'report_date' then report_date end asc
但是我也该如何在asc和desc部分中使用变量?
没有Dynamic SQL每个选项,它的子句例如:
Dynamic SQL
ORDER BY case when @var1 = 'priority asc' THEN priority END ASC , case when @var1 = 'priority desc' then priority end DESC, case when @var2 = 'report_date asc' then report_date end ASC, case when @var2 = 'report_date desc' then report_date end DESC