try:
Create or replace procedure proc_check_inventory (v_date2 date, v_ksdh varchar2)
as
currentdate date;
currentstr varchar2(10);
date1 date;
begin
select max(pcjssj) into date1 from sj_pc where ksdh = v_ksdh;
if date1 is null then
   date1 := to_date('1900-1-1','yyyy-mm-dd');
end if;
if v_date2 > sysdate then
    select v_date2 := sysdate;
end if;
if v_date2 > date1 then
currentdate = sysdate;
currentstr = to_char(currentdate,'yyyy-mm-dd');
delete from sj_check_kc_temp where ksdh = v_ksdh;
insert into sj_check_kc_temp(ksdh,sjdh,sjph,check_kc)
select a.ksdh, a.sjdh, a.sjph, sum(nvl(a.rksl,0))
from sj_rkd a
where  a.ksdh = v_ksdh and
a.rkrq > date1 and
a.rkrq <= v_date2 and
not exists(select 1 from sj_pc where sj_pc.ksdh = a.ksdh and sj_pc.sjdh = a.sjdh and sj_pc.sjph = a.sjph)
group by a.ksdh,a.sjdh,a.sjph;
insert into dbo.sj_check_kc_temp(ksdh,sjdh,sjph,check_kc)
select b.ksdh, b.sjdh, b.sjph, sum(nvl(a.rksl,0))+nvl(b.sjsl,0)
from sj_rkd a, sj_pc b
where  
b.ksdh = a.ksdh(+) and
b.sjdh = a.sjdh(+) and
b.sjph = a.sjph(+) and
b.ksdh = v_ksdh and
a.rkrq > date1 and
a.rkrq <= v_date2
group by b.ksdh,b.sjdh,b.sjph,b.sjsl;
delete from sj_pcb where pcbh = currentstr and ksdh =v_ksdh;
insert into sj_pcb(pcbh,ksdh,sjdh,sjph,pcqssj,pcjssj,kcsl,sjsl)
select  currentstr,
a.ksdh,
a.sjdh,
a.sjph,
date1,
v_date2,
nvl(a.check_kc,0) - sum(nvl(b.cksl,0)),
nvl(a.check_kc,0) - sum(nvl(b.cksl,0))
from sj_check_kc_temp a, sj_ckd b
where   a.ksdh = b.ksdh(+) and 
a.sjdh = b.sjdh(+) and
a.sjph = b.sjph(+) and
b.ckrq >  date1 and
b.ckrq <= v_date2 and
a.ksdh = @ksdh
group by a.ksdh,a.sjdh,a.sjph,a.check_kc;
delete from sj_pc where ksdh = v_ksdh;
insert into sj_pc(ksdh,sjdh,sjph,pcqssj,pcjssj,sjsl,pcbh)
select ksdh, sjdh,sjph,pcqssj,pcjssj, isnull(sjsl,0),pcbh
from sj_pcb
where pcbh = currentstr and ksdh = v_ksdh; end if; end proc_check_inventory;
/

解决方案 »

  1.   

    Create procedure dbo.proc_check_inventory( date2 date, ksdh varchar2(10))as
    currentdate date;
    currentstr varchar2(10);
    date1 date;begin
    select max(pcjssj) into date1 from dbo.sj_pc where ksdh=ksdh; if date1 is null then
         date1:=to_date('1900-1-1','yyyy-mm-dd');
      end if; if (date2 > sysdate) then
    date2: = sysdate;
      end if; if (date2 > date1) then
    currentdate: = sysdate;
    currentstr:=to_char(currentdate);
        delete from dbo.sj_check_kc_temp where ksdh = ksdh;
        
        insert into dbo.sj_check_kc_temp(ksdh,sjdh,sjph,check_kc)
        select a.ksdh , a.sjdh , a.sjph , sum(isnull(a.rksl,0)) into check_kc
    from dbo.sj_rkd a
    where a.ksdh = ksdh and a.rkrq > date1 and a.rkrq <= date2 and
        not exists(select sjph from dbo.sj_pc b 
        where b.ksdh = a.ksdh and b.sjdh = a.sjdh and b.sjph = a.sjph)
        group by a.ksdh,a.sjdh,a.sjph; insert into dbo.sj_check_kc_temp(ksdh,sjdh,sjph,check_kc) select b.ksdh, b.sjdh ,b.sjph,sum(nvl(a.rksl,0)) +nvl(b.sjsl,0)
    from dbo.sj_rkd a, dbo.sj_pc b
    where b.ksdh=a.ksdh(+) and b.sjdh=a.sjdh(+) and b.sjph=a.sjph(+) and
    b.ksdh = ksdh and a.rkrq > date1 and a.rkrq <= date2
    group by b.ksdh,b.sjdh,b.sjph,b.sjsl; delete from dbo.sj_pcb where pcbh =currentstr and  ksdh =ksdh; insert into dbo.sj_pcb(pcbh,ksdh,sjdh,sjph,pcqssj,pcjssj,kcsl,sjsl)
    select  currentstr,a.ksdh,a.sjdh,a.sjph,date1,date2,
    nvl(a.check_kc,0) - sum(nvl(b.cksl,0)),
    nvl(a.check_kc,0) - sum(nvl(b.cksl,0))
    from dbo.sj_check_kc_temp a, dbo.sj_ckd b
    where a.ksdh = b.ksdh(+) and a.sjdh = b.sjdh(+)and
    a.sjph = b.sjph(+) and b.ckrq > date1 and
    b.ckrq <= date2 and a.ksdh = ksdh
    group by a.ksdh,a.sjdh,a.sjph,a.check_kc delete from dbo.sj_pc where ksdh =ksdh; insert into dbo.sj_pc(ksdh,sjdh,sjph,pcqssj,pcjssj,sjsl,pcbh)
        select ksdh, sjdh,sjph,pcqssj,pcjssj, nvl(sjsl,0),pcbh
        from dbo.sj_pcb where   pcbh = currentstr and ksdh =ksdh end if;end;