Oracle在选择中的排序取决于参数

时间:2018-07-18 08:57:22

标签: oracle plsql

我有返回表的流水线函数,看起来像那里:

type transactions is record (
  trans_date date,
  terminal varchar2(20)
);
type transactions_rec_list is table of transactions;

function get_transactions_by_card (
  in_idcard in number,
  in_datefrom in date,
  in_dateto in date,
  in_order_by_id in varchar2
)
return transactions_rec_list parallel_enable pipelined is
begin
  for trns in (
    select   
      t.trans_date,
      t.terminal
    from transactions t
    where t.idcard = in_idcard 
      and t.date >= in_datefrom
      and t.date <= in_dateto
    order by trans_date desc
  )
  loop pipe row (trns);
  end loop;
end;

如何选择order by descasc取决于参数in_order_by_id的值

1 个答案:

答案 0 :(得分:1)

尝试一下:

select   
  t.trans_date,
  t.terminal
from transactions t
where t.idcard = in_idcard 
  and t.date >= in_datefrom
  and t.date <= in_dateto
order by 
  case when in_order_by_id = 'desc' then trans_date end desc,
  case when in_order_by_id = 'asc' then trans_date end asc