--测试数据
create table table2(ccode varchar2(10), cnumber varchar2(10));
insert into table2
select '01','50' from dual union all 
select '02','38' from dual union all
select '03','23' from dual union all
select '04','24' from dual;--存储过程
create or replace procedure getRstData( rst out sys_refcursor)
is
begin
declare
cursor cur is select ccode from table2;
str varchar2(4000);
fcode varchar2(10);
begin
open cur;
loop
fetch cur into fcode;
exit when cur%notfound;
str:=str||',sum(decode(ccode,'||chr(39)||fcode||chr(39)||',cnumber,0)) "'||fcode||'"';
end loop;
str:='select '||substr(str,2,length(str)-1)||' from table2';
dbms_output.put_line(str);
close cur;
open rst for str;
end;
end;--输出结果
01  02  03  04
--------------
50  38  23  24