2011年5月25日 星期三

sys_refcursor with using bing variables

create or replace procedure bm_proc_capa(pWERKS varchar2,pTYPE varchar2,pSMALL_GROUP varchar2,pMPIDGROUP varchar2,
                                         pCURSOR out sys_refcursor) is
  cursor c1(aWERKS varchar2) is
  select *
    from bm_tbl_capa
   where werks = pWERKS
   order by seqno;
  
  cursor c2(aCAPA number,aWERKS varchar2,aSMALL_GROUP varchar2,aTYPE varchar2) is
  with mv as (
              select mpidgroup,cnm,equip,uph,small_group,anln1,werks,
                     vdrnm,mdlnm,locat,sprnm,opmchr,
                     sum(uph*decode(aTYPE,'5',20,'6',22)) over(partition by mpidgroup order by uph desc,opmchr desc rows between unbounded preceding and current row) upd,
                     sum(uph*decode(aTYPE,'5',20,'6',22)) over(partition by mpidgroup order by uph desc,opmchr desc rows between unbounded preceding and 1 preceding) upd1,
                     1/a.opmchr op_d,1/a.opmchr op_n
                from dwmgr.mes_vw_equip_capa a
               where small_group = aSMALL_GROUP
                 and uph is not null
                 and werks = aWERKS
                 and not exists (select null
                                   from bm_tmp_capa b
                                  where a.werks = b.werks
                                    and a.equip = b.equip
                                )
             )
  select a.*,
         max(upd) over(partition by small_group,mpidgroup) upd_max,
         b.seqno
    from mv a,dm_tbl_ktsch_seq b
   where (aCAPA between upd1 and upd or aCAPA > upd)
     and a.mpidgroup = b.ktsch(+)
   order by b.seqno,upd;
begin
  delete from bm_tmp_capa;
  for r1 in c1(pWERKS) loop
    for r2 in c2(r1.capacity,r1.werks,r1.small_group,pTYPE) loop
      insert into bm_tmp_capa(mpidgroup,cnm,equip,uph,small_group,anln1,werks,vdrnm,mdlnm,locat,sprnm,opmchr,
                              upd,upd1,op_d,op_n,upd_max,seqno
                             )
      values(r2.mpidgroup,r2.cnm,r2.equip,r2.uph,r2.small_group,r2.anln1,r2.werks,
             r2.vdrnm,r2.mdlnm,r2.locat,r2.sprnm,r2.opmchr,
             r2.upd,r2.upd1,r2.op_d,r2.op_n,r2.upd_max,r2.seqno
            );
    end loop;
  end loop;
  commit;
  open pCURSOR for 'select * from bm_tmp_capa where small_group like nullif(:small_group,''ALL'')||''%'' and mpidgroup like nullif(:mpidgroup,''ALL'')||''%''' using pSMALL_GROUP,pMPIDGROUP;
end bm_proc_capa;

沒有留言:

張貼留言