【发布时间】:2018-08-03 07:56:57
【问题描述】:
我正在为我的 PLSQL 查询based on this answer(以及许多其他类似的)动态设置要排序的列。我想添加一个默认情况,它按三列排序。
这是我的尝试:
-- ... long SQL query
-- ps_global_order is the column to sort on, passed in to stored procedure.
-- ps_global_order_dir is the direction to sort on, similarly passed in.
order by
case
-- this does not compile (00905 missing keyword):
when ps_global_order is null then name, phone, email
-- this compiles, but I need it on the three columns:
-- when ps_global_order is null then name
end,
case
when ps_global_order_dir <> 'ASC' then ''
when ps_global_order like 'name' then name
end,
case
when ps_global_order_dir <> 'ASC' then ''
when ps_global_order like 'phone' then phone
end,
case
when ps_global_order_dir <> 'ASC' then ''
when ps_global_order like 'email' then email
end,
case
when ps_global_order_dir <> 'DESC' then ''
when ps_global_order like 'name' then name
end,
case
when ps_global_order_dir <> 'DESC' then ''
when ps_global_order like 'phone' then phone
end,
case
when ps_global_order_dir <> 'DESC' then ''
when ps_global_order like 'email' then email
end
一切正常,但我想指定这三列进行排序。
CASE 语句可以做到这一点吗?我必须求助于动态 SQL 还是有我遗漏的解决方法?
【问题讨论】:
-
谢谢 chakeda。绑定变量可用于替换文字,但不能用于静态编译中使用的表、列等 sql 对象。我相信需要动态 sql 来替换列名。