大虾,就拜托妮了/
--存储过程,实现单条记录交叉
create or replace procedure cross_wljy(
v_Sqlcode out number,
v_sqlerrm out varchar2,
v_ReturnCode out number,
v_ReturnMss out varchar2,
v_zbfl number,
v_jyid number,
v_wllx number
) is
--游标
cursor zbmc_cur is
select a.zbid,b.zbmc
from zbfl02 a,zbzd b
where a.zbflid=v_zbfl and a.zbid=b.zbid;
v_cursor number;
v_zbid number;
v_zbmc varchar2(30);
v_RowNum integer;
v_DropStr varchar2(100);
v_CreateStr varchar2(800);
v_alterStr varchar2(100);
v_updateStr varchar2(200);
begin
v_SqlCode:=0;
v_sqlerrm:='';
v_ReturnCode:=-1;
v_ReturnMss:='';
v_cursor:=dbms_sql.open_cursor;
--删除表
begin
v_DropStr:='drop table crosstab_wljy';
dbms_sql.parse(v_cursor,v_DropStr,dbms_sql.v7);
v_RowNum:=dbms_sql.execute(v_cursor);
exception
when Others then
if sqlcode!=-942 then
raise;
end if;
end;
--建表
if v_wllx=0 then
v_CreateStr:='create table crosstab_wljy as '||
' select ....';
end if;
if v_wllx=1 then
v_CreateStr:='create table crosstab_wljy as '||
' select ....';
end if;
dbms_sql.parse(v_cursor,v_CreateStr,dbms_sql.v7);
v_RowNum:=dbms_sql.execute(v_cursor);
--打开游标
open zbmc_cur;
loop
fetch zbmc_cur into v_zbid,v_zbmc;
exit when zbmc_cur%notFound;
--在表crosstab_wljy中添加字段
v_alterStr:='alter table crosstab_wljy add '||v_zbmc||' number(1)';
dbms_sql.parse(v_cursor,v_alterStr,dbms_sql.v7);
v_RowNum:=dbms_sql.execute(v_cursor);
--给新加字段赋值
v_updateStr:='update crosstab_wljy'||
' set '||v_zbmc||'=(select zbnr from jy02 where jyid='||v_jyid||
' and zbid='||v_zbid||')';
dbms_sql.parse(v_cursor,v_updateStr,dbms_sql.v7);
v_RowNum:=dbms_sql.execute(v_cursor);
end loop;
dbms_sql.close_cursor(v_cursor);
close zbmc_cur;
commit;
v_ReturnCode:=0;
exception
when Others then
v_sqlcode:=SqlCode;
v_sqlerrm:=SqlErrm;
close zbmc_cur;
dbms_sql.close_cursor(v_cursor);
rollback;
raise;
end cross_wljy;