按顺序使用 asc 和 desc 变量 [英] Using variables for asc and desc in order by

查看:40
本文介绍了按顺序使用 asc 和 desc 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以像这样按 sql 查询部分的顺序使用变量:

I understand that I can use variables in the order by section of sql queries like this:

order by 
case when @var1 = 'priority' then priority end desc,
case when @var2 = 'report_date' then report_date end asc

但是我如何也为 asc 和 desc 部分使用变量?

But how do I use variables for the asc and desc sections too?

推荐答案

without Dynamic SQL 每个选项的子句例如:

without Dynamic SQL each option it's clause for example:

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

这篇关于按顺序使用 asc 和 desc 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆