我写一存储过程,并在调试中输入参数2008,4,在sqlplus中select a.DELIVEROUTPUTID,a.projectid from R_DELIVEROUTPUT a where a.BELONGYEAR=2008 and a.BelongMonth=4 能检索到记录,但在存储过程中EXIT WHEN source%NOTFOUND;却直接跳出,为什么?
CREATE OR REPLACE PROCEDURE VPREPORT.P_CREATEDELIVEROUTPUTDETAIL
(BelongYear1 in NUMBER,BelongMonth1 in NUMBER) is
DELIVEROUTPUTDETAILID varchar2(40) :='';
DELIVEROUTPUTID1 varchar2(40) :='';
projectid1 varchar2(40) :='';
CURSOR source IS select a.DELIVEROUTPUTID,a.projectid from R_DELIVEROUTPUT a where a.BELONGYEAR=BelongYear1 and a.BelongMonth=BelongMonth1;
begin
open source;
LOOP
FETCH source INTO DELIVEROUTPUTID1,projectid1;
EXIT WHEN source%NOTFOUND;
--插入工时
insert into R_DELIVEROUTPUTDETAIL(DELIVEROUTPUTDETAILID,DELIVEROUTPUTID,CENTERCODE,CENTERINFONAME,TOTALNUMBER,type) select DELIVEROUTPUTDETAILID,DELIVEROUTPUTID1,B.centerCode,B.centerName,nvl((select sum(ImplementTime) from R_DEPARTMENTTHROW where ProjectId=projectid1 and centerCode=b.centerCode and BelongYear=BelongYear1 and BelongMonth=BelongMonth1 ),0) as ImplementTime,1 from (select distinct a.centerCode,a.centerName from R_AVAILABLEDAY a where to_char(a.startDate,'YYYY')=BelongYear1 and to_char(a.startDate,'MM')=BelongMonth1 and to_char(a.endDate,'YYYY')=BelongYear1 and to_char(a.endDate,'MM')=BelongMonth1 group by a.centerCode,a.centerName) b order by b.centerCode;
--插入成本
insert into R_DELIVEROUTPUTDETAIL(DELIVEROUTPUTDETAILID,DELIVEROUTPUTID,CENTERCODE,CENTERINFONAME,TOTALNUMBER,type) select DELIVEROUTPUTDETAILID,DELIVEROUTPUTID1,B.centerCode,B.centerName,nvl((select sum(Cost) from R_EmployeeCostIncome where ProjectId=ProjectId1 and centerCode=b.centerCode and BelongYear=BelongYear1 and BelongMonth=BelongMonth1 ),0) as Cost ,2 from (select distinct a.centerCode,a.centerName from R_AVAILABLEDAY a where to_char(a.startDate,'YYYY')=BelongYear1 and to_char(a.startDate,'MM')=BelongMonth1 and to_char(a.endDate,'YYYY')=BelongYear1 and to_char(a.endDate,'MM')=BelongMonth1 group by a.centerCode,a.centerName) b;
--插入收入
insert into R_DELIVEROUTPUTDETAIL(DELIVEROUTPUTDETAILID,DELIVEROUTPUTID,CENTERCODE,CENTERINFONAME,TOTALNUMBER,type) select DELIVEROUTPUTDETAILID,DELIVEROUTPUTID1,B.centerCode,B.centerName,nvl((select sum(Income) from R_EmployeeCostIncome where ProjectId=ProjectId1 and centerCode=b.centerCode and BelongYear=BelongYear1 and BelongMonth=BelongMonth1),0) as Income,3 from (select distinct a.centerCode,a.centerName from R_AVAILABLEDAY a where to_char(a.startDate,'YYYY')=BelongYear1 and to_char(a.startDate,'MM')=BelongMonth1 and to_char(a.endDate,'YYYY')=BelongYear1 and to_char(a.endDate,'MM')=BelongMonth1 group by a.centerCode,a.centerName) b;
END LOOP;
CLOSE source;
--result :=1;
-- 异常处理
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
--result :=0; END;
CREATE OR REPLACE PROCEDURE VPREPORT.P_CREATEDELIVEROUTPUTDETAIL
(BelongYear1 in NUMBER,BelongMonth1 in NUMBER) is
DELIVEROUTPUTDETAILID varchar2(40) :='';
DELIVEROUTPUTID1 varchar2(40) :='';
projectid1 varchar2(40) :='';
CURSOR source IS select a.DELIVEROUTPUTID,a.projectid from R_DELIVEROUTPUT a where a.BELONGYEAR=BelongYear1 and a.BelongMonth=BelongMonth1;
begin
open source;
LOOP
FETCH source INTO DELIVEROUTPUTID1,projectid1;
EXIT WHEN source%NOTFOUND;
--插入工时
insert into R_DELIVEROUTPUTDETAIL(DELIVEROUTPUTDETAILID,DELIVEROUTPUTID,CENTERCODE,CENTERINFONAME,TOTALNUMBER,type) select DELIVEROUTPUTDETAILID,DELIVEROUTPUTID1,B.centerCode,B.centerName,nvl((select sum(ImplementTime) from R_DEPARTMENTTHROW where ProjectId=projectid1 and centerCode=b.centerCode and BelongYear=BelongYear1 and BelongMonth=BelongMonth1 ),0) as ImplementTime,1 from (select distinct a.centerCode,a.centerName from R_AVAILABLEDAY a where to_char(a.startDate,'YYYY')=BelongYear1 and to_char(a.startDate,'MM')=BelongMonth1 and to_char(a.endDate,'YYYY')=BelongYear1 and to_char(a.endDate,'MM')=BelongMonth1 group by a.centerCode,a.centerName) b order by b.centerCode;
--插入成本
insert into R_DELIVEROUTPUTDETAIL(DELIVEROUTPUTDETAILID,DELIVEROUTPUTID,CENTERCODE,CENTERINFONAME,TOTALNUMBER,type) select DELIVEROUTPUTDETAILID,DELIVEROUTPUTID1,B.centerCode,B.centerName,nvl((select sum(Cost) from R_EmployeeCostIncome where ProjectId=ProjectId1 and centerCode=b.centerCode and BelongYear=BelongYear1 and BelongMonth=BelongMonth1 ),0) as Cost ,2 from (select distinct a.centerCode,a.centerName from R_AVAILABLEDAY a where to_char(a.startDate,'YYYY')=BelongYear1 and to_char(a.startDate,'MM')=BelongMonth1 and to_char(a.endDate,'YYYY')=BelongYear1 and to_char(a.endDate,'MM')=BelongMonth1 group by a.centerCode,a.centerName) b;
--插入收入
insert into R_DELIVEROUTPUTDETAIL(DELIVEROUTPUTDETAILID,DELIVEROUTPUTID,CENTERCODE,CENTERINFONAME,TOTALNUMBER,type) select DELIVEROUTPUTDETAILID,DELIVEROUTPUTID1,B.centerCode,B.centerName,nvl((select sum(Income) from R_EmployeeCostIncome where ProjectId=ProjectId1 and centerCode=b.centerCode and BelongYear=BelongYear1 and BelongMonth=BelongMonth1),0) as Income,3 from (select distinct a.centerCode,a.centerName from R_AVAILABLEDAY a where to_char(a.startDate,'YYYY')=BelongYear1 and to_char(a.startDate,'MM')=BelongMonth1 and to_char(a.endDate,'YYYY')=BelongYear1 and to_char(a.endDate,'MM')=BelongMonth1 group by a.centerCode,a.centerName) b;
END LOOP;
CLOSE source;
--result :=1;
-- 异常处理
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
--result :=0; END;
确定BELONGYEAR是数字型的吗
还有,source是关键字,用它做游标名不大好吧,不知道会不会有问题
你在loop里一开始就打出一句话,看看进没进loop