這兩天工作還行,工作的內(nèi)容就是寫存儲(chǔ)過程,對(duì)于我這個(gè)沒有寫過存儲(chǔ)近程的人來說,還是有一定困難的.不過還好,在眾多資源的幫助下,萬(wàn)事OK呀,哈哈.下面就是我寫的兩個(gè)存儲(chǔ)過程.

       其一:對(duì)數(shù)據(jù)庫(kù)中的原表進(jìn)行每天的備份;

       其二:通過對(duì)最新的備份表進(jìn)行取值操作,通過判斷,向原表中插入數(shù)據(jù).

       其實(shí)這兩個(gè)存儲(chǔ)過程也沒有什么難的,一方面我是這方面的新手,另一方面要操作一個(gè)臨時(shí)表,而且要從中取得數(shù)據(jù)(沒有傳入值),我在網(wǎng)上找了一下,沒有什么成功的例子,所以,把我這兩天的成果曬一下.

       先說幾個(gè)關(guān)鍵詞(我用的時(shí)候,沒少為這幾個(gè)詞費(fèi)腦子)

1.       execute Immediate,一個(gè)執(zhí)行動(dòng)態(tài)SQL的東東,在每天生成備份表時(shí),立下了漢馬功勞.我的表名為:原表名+yyyymmdd

2.       creat table 表名 as select * from 已有表.這是一個(gè)創(chuàng)建表,我認(rèn)為最快的一種方法,在創(chuàng)建表的同時(shí),也可以直接把已有表中的數(shù)據(jù)也一起copy過來,呵呵,很是神奇

3.       sys_refcursor,這是一個(gè)cursor,很怪異的一個(gè)cursor,能夠生成動(dòng)態(tài)的cursor,可以多用一些,

 

下面我寫的存儲(chǔ)過程,主要地業(yè)務(wù)邏輯去掉了,主要說一下過程

1.      每天生成表備份的存儲(chǔ)過程

create or replace procedure P_HOLD_COPY Is
--每天結(jié)算時(shí),為當(dāng)天的持股做備份
--得到表名為原表名+yyyymmdd的格式
table_name Varchar2(20) := 'test'||To_char(Sysdate,'yyyymmdd');
Begin
    execute Immediate 'Create Table '||table_name||' As Select * From T_STOC_HOLD';
  Commit;
end P_HOLD_COPY;

2.       從備份表中取得數(shù)據(jù),然后根據(jù)數(shù)據(jù),對(duì)原表進(jìn)行操作

create or replace procedure P__SHARE  is
--******************************************************************
--存儲(chǔ)過程名稱:P_SHARE
--功能描述:對(duì)備份表進(jìn)行數(shù)據(jù)操作
--******************************************************************
 balance Number;
 totalmoney Number;
 h_amount Number;
 
 allot_stock Number;
 allot_money Number;
 
 str Varchar2(1);
 v_product_oid Varchar2(32);
 
table_name Varchar2(20);
sqlstr Varchar2(1000);
 
 v_allot_row t_stoc_allot%rowtype;
 v_hold_row t_stoc_hold%rowtype;

 Cursor c_allot is select * from t_stoc_allot where to_char(t_stoc_allot.regdate,'yymmdd') < to_char(sysdate+1,'yymmdd') And (t_stoc_allot.is_allot Is Null Or t_stoc_allot.is_allot='1');

 c_hold sys_refcursor  ;

Begin

  open c_allot;
  loop
      fetch c_allot into v_allot_row;
       
      table_name :=  'T_STOC_HOLD'||To_char(v_allot_row.regdate,'yyyymmdd');
     
      v_product_oid := v_allot_row.product_oid;
      sqlstr := 'select * from '||table_name||' where stock_code='||v_product_oid;
      exit when c_allot%NOTFOUND;

        open c_hold For sqlstr ;
        loop
            fetch c_hold into v_hold_row;
            exit when c_hold%NOTFOUND;
            
                   end loop;
        close c_hold;
          end loop;
  close c_allot;
 
  Commit;
end P_SHARE;