Oracle ORA-22922 错误

测试环境是Oracle 10g 10.2.0.4 :

select *
from (select t.*, ROWNUM num
from (select t.user_type,
t.producttype_name,
t.prodname,
t.comcname,
sum(t.cnt) cnt,
sum(t.premium) premium,
cast(WM_CONCAT(policytype || '-' || cnt) as
varchar2(300)) policytype,
cast(WM_CONCAT(status) as varchar2(300)) status,
cast(WM_CONCAT(t.premium) as varchar2(300)) dayPremium,
t.username
from (select u.user_type,
c.comcname,
protype.producttype_name,
product.prodname,
decode(policy.policy_type,
'0',
'0',
'1',
'1',
'2') policytype,
count(POLICYKEY) cnt,
sum(policy.premium) premium,
POLICY_STATUS || PROCESS_STATE status,
u.username
from T_POLICY policy,
T_PRODUCT product,
T_PRODUCT_TYPE protype,
T_USER u,
T_COMPANY c
where policy.product_id = product.product_id
and product.prodtype = protype.producttype_code
and u.usercode = policy.OPERATOR
and c.comcode = policy.MAKE_COM
and policy.CREATE_TIME BETWEEN
to_date('2012-01-01', 'yyyy-MM-dd') and
to_date('2012-04-12' || ' 23:59:59',
'yyyy-MM-dd hh24:mi:ss')
and policy.MAKE_COM in
(select COMCODE
from T_COMPANY
start with COMCODE = '18000000'
connect by prior COMCODE = UPPERCOMCODE)
group by u.user_type,
c.comcname,
protype.producttype_name,
product.prodname,
policy.policy_type,
POLICY_STATUS || PROCESS_STATE,
u.username) t
group by t.user_type,
comcname,
t.producttype_name,
t.prodname,
t.username) t
WHERE ROWNUM <= 15)
WHERE num >= 1

运行这个分页查询没问题

但是在生产环境10.2.0.5上运行报ORA-22922 错误,分析一下原因 就是

cast(WM_CONCAT(policytype || '-' || cnt) as
varchar2(300)) policytype,
cast(WM_CONCAT(status) as varchar2(300)) status,
cast(WM_CONCAT(t.premium) as varchar2(300)) dayPremium 这个WM_CONCAT导致的,这是oracle undocumented的一个列转行的函数,这个函数会把varchar的字段合为lob,但是奇怪的是即使用cast函数强转还是用substr和to_char结合 都解决不了这个问题,有itpub上的朋友自己重写了一个列转行的函数:

create or replace TYPE zh_concat_im
AUTHID CURRENT_USER AS OBJECT
(
CURR_STR VARCHAR2(32767),
STATIC FUNCTION ODCIAGGREGATEINITIALIZE(SCTX IN OUT zh_concat_im) RETURN NUMBER,
MEMBER FUNCTION ODCIAGGREGATEITERATE(SELF IN OUT zh_concat_im,
P1 IN VARCHAR2) RETURN NUMBER,
MEMBER FUNCTION ODCIAGGREGATETERMINATE(SELF IN zh_concat_im,
RETURNVALUE OUT VARCHAR2,
FLAGS IN NUMBER)
RETURN NUMBER,
MEMBER FUNCTION ODCIAGGREGATEMERGE(SELF IN OUT zh_concat_im,
SCTX2 IN zh_concat_im) RETURN NUMBER
);
/

create or replace TYPE BODY zh_concat_im
IS
STATIC FUNCTION ODCIAGGREGATEINITIALIZE(SCTX IN OUT zh_concat_im)
RETURN NUMBER
IS
BEGIN
SCTX := zh_concat_im(NULL) ;
RETURN ODCICONST.SUCCESS;
END;
MEMBER FUNCTION ODCIAGGREGATEITERATE(SELF IN OUT zh_concat_im,
P1 IN VARCHAR2)
RETURN NUMBER
IS
BEGIN
IF(CURR_STR IS NOT NULL) THEN
CURR_STR := CURR_STR || ':' || P1;
ELSE
CURR_STR := P1;
END IF;
RETURN ODCICONST.SUCCESS;
END;
MEMBER FUNCTION ODCIAGGREGATETERMINATE(SELF IN zh_concat_im,
RETURNVALUE OUT VARCHAR2,
FLAGS IN NUMBER)
RETURN NUMBER
IS
BEGIN
RETURNVALUE := CURR_STR ;
RETURN ODCICONST.SUCCESS;
END;
MEMBER FUNCTION ODCIAGGREGATEMERGE(SELF IN OUT zh_concat_im,
SCTX2 IN zh_concat_im)
RETURN NUMBER
IS
BEGIN
IF(SCTX2.CURR_STR IS NOT NULL) THEN
SELF.CURR_STR := SELF.CURR_STR || ':' || SCTX2.CURR_STR ;
END IF;
RETURN ODCICONST.SUCCESS;
END;
END;
/

函数:
create or replace FUNCTION zh_concat(P1 VARCHAR2)
RETURN VARCHAR2 AGGREGATE USING zh_concat_im ;
/

效率不如WM_CONCAT但是还是解决了这个问题,感谢这位网友啊!

你可能感兴趣的:(oracle)