Oracle的PL/SQL編程之基礎技能實戰一 一>基礎代碼檢查 檢查以bm_開頭的系統初始化編碼表是否有空值。與業務系統相關的編碼項不能存在空值,會致使系統業務沒法辦理。爲初始化數據表、在作測試數據和正式上線前檢查。上線運行後、仍存在空值表、須要進行覈實、可能存在不常常辦理的業務。也多是冗餘表。 PL/SQL代碼塊: declare v_table_name varchar(40); v_sql_str varchar(4000):=' '; v_cnt smallint:=0 ; v_jgbm varchar(10):='01%'; cursor tmp_cur is select table_name from user_tables where table_name like 'BM_%' ; begin open tmp_cur ; loop fetch tmp_cur into v_table_name ; exit when tmp_cur%notfound; select count(*) into v_cnt from user_tab_columns where table_name=v_table_name and column_name='JGBM'; if v_cnt<>0 then v_sql_str:='select count(*) from '||v_table_name||' where jgbm='''||v_jgbm||''''; else v_sql_str:='select count(*) from '||v_table_name; end if; execute immediate v_sql_str into v_cnt; if v_cnt=0 then insert into tmp_hfsc_sy40_chk(ywms) values(v_table_name||'表記錄爲空'); end if; commit; end loop; close tmp_cur; end; 二>財務業務覈對 業務關於資金的結算都是自動生成憑證,財務和業務應是保持一致的。如存在特殊業務可能會有不平衡的狀況。若是有人調整數據也會形成不平。主要檢查 歸集餘額、單位未分配金額、貸款餘額、逾期貸款、業務流水金額和財務憑證金額是否相等。直接影響到系統的報表業務和財務的一致性。 PL/SQL代碼塊: declare v_fpzd smallint; v_ztbh number(20); v_cwnd smallint; v_kmye number(18,2); v_gjye number(18,2); v_jgbm varchar(10):='01%'; v_msg varchar(100); v_ret smallint:=0; begin select nvl(max(value1),0) into v_fpzd from bm_xtcs where bm1=v_jgbm and bm2='0301' and bm3='03010903' and bm='04' and sfqy=1; for a in(select id from cw_ztml where ztxz='01' and jgbm=v_jgbm) loop select max(nd) into v_cwnd from cw_nd where ztbh=a.id; select nvl(sum(nce+ljdf-ljjf),0) into v_kmye from cw_kmbh where kmbh='201' and ztbh=a.id and nd=v_cwnd; select v_kmye+nvl(sum(fl.dffse-fl.jffse),0) into v_kmye from cw_pz_fl fl inner join cw_pz_ml b on fl.pzid=b.pzid where kmbh like '201%' and b.ztbh=a.id and b.nd=v_cwnd and b.zfbz=0; if v_fpzd<>0 then select nvl(sum(dwzhye),0) into v_gjye from gjzf_dw_zz where jgbm=v_jgbm; else select nvl(sum(zckye+dwzhye),0) into v_gjye from gjzf_dw_zz where jgbm=v_jgbm; end if; if v_kmye<>v_gjye then insert into tmp_hfsc_sy40_chk(ywms) values(to_char(a.id)||'帳套'||'201科目餘額與業務歸集餘額不一致'); end if; commit; end loop; end; 三>跟新im_zhsz_dw中的yhmc使得同一銀行帳號和grdk_xm_zhlr中的skyhmc相等. declare v_yhmc varchar2(40) := ' '; v_skyhzh varchar2(50) := ' '; v_count number(20):=0; cursor tmp_cur is select distinct a.yhmc,b.skyhmc, b.skyhzh from im_zhsz_dw a inner join grdk_xm_zhlr b on a.yhzhhm = b.skyhzh and a.yhmc=b.skyhmc; begin open tmp_cur; loop fetch tmp_cur into v_yhmc, v_skyhzh; exit when tmp_cur%notfound; update im_zhsz_dw a set a.yhmc = v_yhmc where a.yhzhhm = v_skyhzh and a.yhmc <> v_yhmc; v_count:=v_count+1; end loop; close tmp_cur; dbms_output.put_line('一共更新了'||v_count||'行'); end;
總結:PL/SQL編程解決問題的通常步驟:sql
1>建立存儲空間表,要定義哪些字段以及類型以及約束;編程
2>若是這些表裏面沒有數據,要向這些表裏面準備數據;ide
3>理清業務邏輯,操做這些表裏面的數據邏輯;
oop
4>得到結果數據;測試