學習儲存過程時寫的個例子

gjm008發表於2008-12-28

RK表的欄位變化時,插入細表的記錄到另一個表中:

create or replace procedure mxz2(bill in varchar2) is
  cursor curbill is
    select style, qty, taxcost,amnt from rks where rkbill = bill;
  csstyle. varchar2(15);
  csqty   number(18, 2);
  cscost  number(18, 2);
  seqno   number(18);
  sty_seqno number(18);
  vdate   date;
  jamnt  number(18,2);
  jcqty  number(18,2);
  jcamnt number(18,2);
begin
  -- select operdate into vdate from rk where rkbill=bill;
   --當表觸發器中呼叫儲存過程時,不能在這裡寫呼叫原表的欄位資訊。
   vdate:='2008-10-01';
  open curbill;
  loop
    fetch curbill
      into csstyle, csqty, cscost,jamnt;
    exit when curbill%notfound;
    begin
      select max(maxseqno) into sty_seqno from mxz where style. =csstyle;
      if (sty_seqno is null) then
       begin
        jc_qty:=0;
        jc_amnt:=0; 
       end;
      else
       select jcqty,jctamnt into jcqty,jcamnt from mxz where maxseqno=sty_seqno;
      end if ;
     
      select max(maxseqno) into seqno from mxz ;
      if (seqno is null) then
         seqno:=1;
      else  
         seqno:=seqno+1;
      end if ;
      insert into mxz
        (maxseqno,style,validdate,cost,qty,jamnt,jcqty,jcamnt)
      values
        (seqno,csstyle,vdate,cscost,csqty,jamnt,jcqty+csqty,jcamnt+jamnt );
    
    end;
  end loop;
  close curbill;

 commit;  ---提交事務--觸發器呼叫時,也不允許有事務存在!
 
 exception
   when others then
 rollback;
 
end mxz2;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16486600/viewspace-524505/,如需轉載,請註明出處,否則將追究法律責任。

相關文章