【问题标题】:PLSQL: Multiple values in CASE resultPLSQL:CASE 结果中的多个值
【发布时间】: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 来替换列名。

标签: plsql case


【解决方案1】:

这样做:

-- ... 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 
    when ps_global_order is null then name
    else null
  end,
  case
    when ps_global_order is null then phone
    else null
  end,
  case
    when ps_global_order is null then email
    else null
  end,
  case
    when ps_global_order_dir <> 'ASC' then null
    when ps_global_order like 'name' then name
  end,
  case
    when ps_global_order_dir <> 'ASC' then null
    when ps_global_order like 'phone' then phone
  end,
  case
    when ps_global_order_dir <> 'ASC' then null
    when ps_global_order like 'email' then email
  end,
  case
    when ps_global_order_dir <> 'DESC' then null
    when ps_global_order like 'name' then name
  end,
  case
    when ps_global_order_dir <> 'DESC' then null
    when ps_global_order like 'phone' then phone
  end,
  case
    when ps_global_order_dir <> 'DESC' then null
    when ps_global_order like 'email' then email 
  end

请注意,我已将所有出现的 '' 替换为 NULL,这更清楚 IMO。

【讨论】:

    【解决方案2】:

    我讨厌这么快回答我自己的问题,但我想通了:

    order by 
        case  when nvl(ps_global_order, ' ') = ' ' then name end,
        case  when nvl(ps_global_order, ' ') = ' ' then phone end,
        case  when nvl(ps_global_order, ' ') = ' ' then email end,
        case
            when ps_global_order_dir <> 'ASC' then ''
        -- continues...
    

    This answer 提供了很大帮助,但它被隐藏在 Google 搜索结果中。不过,没有逗号的情况下它是如何工作的却让我望而却步。

    【讨论】:

    • 逗号在那里。这就是end 之后的内容。
    • 我是这么认为的! PLSQL 很奇怪。
    • 这实际上是直接的 SQL。您正在对一系列 CASE 表达式的结果进行排序。
    • 我现在明白了 - 我认为逗号是 case 语句之间的分隔符。这是漫长的一天
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    相关资源
    最近更新 更多