--創建存儲過程
CREATE OR REPLACE PROCEDURE xxxxxxxxxxx_p( --參數IN表示輸入參數, --OUT表示輸出參數,類型可以使用任意Oracle中的合法類型。 is_ym IN CHAR) AS --定義變量 vs_msg VARCHAR2(4000); --錯誤信息變量 vs_ym_beg CHAR(6); --起始月份 vs_ym_end CHAR(6); --終止月份 vs_ym_sn_beg CHAR(6); --同期起始月份 vs_ym_sn_end CHAR(6); --同期終止月份 --定義游標(簡單的說就是一個可以遍歷的結果集) CURSOR cur_1 IS SELECT area_code, CMCODE, SUM(rmb_amt) / 10000 rmb_amt_sn, SUM(usd_amt) / 10000 usd_amt_sn FROM BGD_AREA_CM_M_BASE_T WHERE ym >= vs_ym_sn_beg AND ym <= vs_ym_sn_end GROUP BY area_code, CMCODE; BEGIN --用輸入參數給變量賦初值,用到了Oralce的SUBSTR TO_CHAR ADD_MONTHS TO_DATE 等很常用的函數。 vs_ym_beg := SUBSTR(is_ym, 1, 6); vs_ym_end := SUBSTR(is_ym, 7, 6); vs_ym_sn_beg := TO_CHAR(ADD_MONTHS(TO_DATE(vs_ym_beg, 'yyyymm'), -12), 'yyyymm'); vs_ym_sn_end := TO_CHAR(ADD_MONTHS(TO_DATE(vs_ym_end, 'yyyymm'), -12), 'yyyymm'); --先刪除表中特定條件的數據。 DELETE FROM xxxxxxxxxxx_T WHERE ym = is_ym; --然後用內置的DBMS_OUTPUT對象的put_line方法打印出影響的記錄行數,其中用到一個系統變量SQL%rowcount DBMS_OUTPUT.put_line('del上月記錄=' || SQL%rowcount || '條'); INSERT INTO xxxxxxxxxxx_T (area_code, ym, CMCODE, rmb_amt, usd_amt) SELECT area_code, is_ym, CMCODE, SUM(rmb_amt) / 10000, SUM(usd_amt) / 10000 FROM BGD_AREA_CM_M_BASE_T WHERE ym >= vs_ym_beg AND ym <= vs_ym_end GROUP BY area_code, CMCODE; DBMS_OUTPUT.put_line('ins當月記錄=' || SQL%rowcount || '條'); --遍歷游標處理後更新到表。遍歷游標有幾種方法,用for語句是其中比較直觀的一種。 FOR rec IN cur_1 LOOP UPDATE xxxxxxxxxxx_T SET rmb_amt_sn = rec.rmb_amt_sn, usd_amt_sn = rec.usd_amt_sn WHERE area_code = rec.area_code AND CMCODE = rec.CMCODE AND ym = is_ym; END LOOP; COMMIT; --錯誤處理部分。OTHERS表示除了聲明外的任意錯誤。SQLERRM是系統內置變量保存了當前錯誤的詳細信息。 EXCEPTION WHEN OTHERS THEN vs_msg := 'ERROR IN xxxxxxxxxxx_p(' || is_ym || '):' || SUBSTR(SQLERRM, 1, 500); ROLLBACK; --把當前錯誤記錄進日志表。 INSERT INTO LOG_INFO (proc_name, error_info, op_date) VALUES ('xxxxxxxxxxx_p', vs_msg, SYSDATE); COMMIT; RETURN; END;--創建函數
create or replace function get_publicholidaytime(fromtime in Date, totime in Date, isAvailable in number) return number as --定義變量 free_day number := 0; mindate Date; maxdate Date; total number := 0; fromdate Date := TO_DATE(to_char(fromtime, 'yyyy/MM/dd'), 'yyyy/MM/dd'); todate Date := TO_DATE(to_char(totime, 'yyyy/MM/dd'), 'yyyy/MM/dd'); begin --if語句 if isAvailable = 1 then --sql語句 select min(calendar_date), max(calendar_date), count(calendar_date) into mindate, maxdate, total from T_BI_TNT_DATE where calendar_date between fromdate and todate and IS_PUBLIC_HOLIDAY = 1; if mindate = fromdate then total := total - 1; free_day := free_day + ((mindate + 1) - fromtime); end if; if maxdate = todate then total := total - 1; free_day := free_day + (totime - maxdate); end if; if mindate = maxdate then free_day := totime - fromtime; else free_day := free_day + total; end if; end if; return free_day; end get_publicholidaytime;相關文檔