在 Oracle 中按 user_tab_partitions 的 HIGHVALUE 排序

order by HIGHVALUE on user_tab_partitions in Oracle

我正在尝试 运行 在我的 PL/SQL 块

中进行以下查询
SELECT partition_name, high_value FROM user_tab_partitions WHERE table_name = 'BRD_JOB_DETAILS_TMP' order by high_value;

我收到错误消息:ORA-00997: illegal use of LONG datatype

我需要按 high_value 的顺序处理 table 个分区,以解决以下问题:

ORA-14074: partition bound must collate higher than that of the last partition

使用数字列 partition_position 代替您的订单。

SELECT partition_name, high_value FROM user_tab_partitions WHERE table_name = 'BRD_JOB_DETAILS_TMP' order by partition_position;

在列表分区的情况下,分区号可能与实际值顺序不一致。但是只要LONG列在SQL中有a lot of restrictions,一般的处理方式是用dbms_xmlgen.getxml[type]long值序列化为文本,然后检索为 varchar2.

下面是一个例子:

create table t (
  id date
)
partition by list(id) (
  partition p20200101 values (date '2020-01-01'),
  partition p20200601 values (date '2020-06-01'),
  partition p20200301 values (date '2020-03-01')
)
with
/*In case of date column high_value contains an expression,
not the value itself.
So evaluation is required.*/

function f_evaluate_val (p_val varchar2)
  return date
/*Evaluates high_value as SQL expression*/
as
  l_res date;
begin
  execute immediate
    'select ' || p_val || ' from dual'
    into l_res;
  return l_res;
end;

a as (
  /*wrap with xmltype as long as LONG columns
  are not accessible for functions in SQL*/
  select dbms_xmlgen.getxmltype(q'{
    select
      table_name,
      partition_name,
      high_value
    from user_tab_partitions
    where table_name = 'T'
  }') as x
  from dual
)

select
  t.table_name,
  t.partition_name,
  f_evaluate_val(t.high_value) as hv
from a
  cross join xmltable(
    '//ROWSET/ROW'
    passing a.x
    columns
      table_name varchar2(30),
      partition_name varchar2(30),
      high_value varchar2(4000)
  ) t
order by hv desc
TABLE_NAME | PARTITION_NAME | HV       
:--------- | :------------- | :--------
T          | P20200601      | 01-JUN-20
T          | P20200301      | 01-MAR-20
T          | P20200101      | 01-JAN-20

db<>fiddle here