資料庫名稱: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 ||') <=這一句
小弟新手卡一段時間了,請各位高手指點迷津, 感謝~