数据库名称:ORACLE
数据库版本:12c
内容/问题描述:小弟我用PIVOT写一个转置的查询Table
原先的语法如下=>
declare
sqlqry clob;
cols clob;
begin
select listagg(''''|| PS_C3 ||'''', ',') within group (order by PS_C3)
into cols
from (select distinct PS_C3 from pick2_1);
sqlqry :=
'
create or replace view pick2_2 as
select * from
(
select num_po, datdels, nam_cust,xfer, PS_C3, qty
from pick2_1
)
pivot
(
sum(qty) for PS_C3 in (' || cols || ')
)';
execute immediate sqlqry;
end;
/
后来RUN发现ps_c3这个字段组成的字串过长 (listagg好像有4000字限制)
于是改用xmlagg的方式组字串
语法如下=>
declare
sqlqry clob;
cols clob;
begin
select
xmlagg(xmlparse(content ''''|| PS_C3 ||''''||',' wellformed) order by
ps_c3).getclobval()
into cols
from (select distinct ps_c3 from pick2_1);
sqlqry :=
'create or replace view pick2_2 as
select * from
(
select num_po, datdels, nam_cust,xfer, PS_C3, qty
from pick2_1
)
pivot
(
sum(qty) for PS_C3 in ( '|| cols ||' )
)';
execute immediate sqlqry;
end;
/
数据库会出现报错讯息"ORA-00936:遗漏表示式"在line24
(sum(qty) from PS_C3 in ('|| cols ||') <=这一句
小弟新手卡一段时间了,请各位高手指点迷津, 感谢~