1. 程式人生 > 實用技巧 >SQL-006以店存量提取資料驗證現存量

SQL-006以店存量提取資料驗證現存量

/* Formatted on 2020/12/08 17:46:58 (QP5 v5.326) */
DECLARE
    CURSOR c_rmb_onhandnum_withnocode IS
        SELECT *
          FROM rmb_onhandnum
         WHERE     (nonhandnum > 0)
               AND (NVL (dr, 0) = 0)
               AND (vbatchcode IS NULL);

    CURSOR c_rmb_onhandnum_withcode IS
        SELECT 
* FROM rmb_onhandnum WHERE (nonhandnum > 0) AND (NVL (dr, 0) = 0) AND (vbatchcode IS NOT NULL); TYPE productinfo IS RECORD ( invclassname VARCHAR2 (240), invcode VARCHAR2 (40), invname VARCHAR2 (200), measname VARCHAR2 (
200), num NUMBER (20, 8), storname VARCHAR2 (200) ); v_product productinfo; v_product1 productinfo; v_product2 productinfo; v_product3 productinfo; num INT :
= 0; i INT := 0; j INT := 0; diffvalue INT := 0; startDate CHAR (30) := '2020-12-31'; endDate CHAR (30) := '2022-12-31'; v_ic_onhandnum ic_onhandnum%ROWTYPE; v_ic_onhandnum_pk_corp ic_onhandnum.pk_corp%TYPE; v_ic_onhandnum_nonhandnum IC_ONHANDNUM.NONHANDNUM%TYPE; v_rmb_onhandnum rmb_onhandnum%ROWTYPE; v_rmb_onhandnum_nonhandnum rmb_onhandnum.nonhandnum%TYPE; v_rmb_onhandnum_cinvmanid rmb_onhandnum.cinvmanid%TYPE; v_rmb_onhandnum_cwhid rmb_onhandnum.cwhid%TYPE; v_rmb_onhandnum_cinvbasid rmb_onhandnum.cinvbasid%TYPE; v_rmb_onhandnum_vbatchcode rmb_onhandnum.vbatchcode%TYPE; BEGIN SELECT TO_CHAR (SYSDATE, 'yyyy-mm-dd hh:mm:ss') INTO endDate FROM DUAL; DBMS_OUTPUT.put_line ( '以店存量提取資料驗證現存量,檢查開始時間--無批次號檢驗:' || endDate); DBMS_OUTPUT.put_line ('==============================================='); DBMS_OUTPUT.put_line ( '===========以下查詢無批次號現存/店存資料==========='); DBMS_OUTPUT.put_line ('==============================================='); IF NOT c_rmb_onhandnum_withnocode%ISOPEN THEN OPEN c_rmb_onhandnum_withnocode; END IF; NULL; LOOP FETCH c_rmb_onhandnum_withnocode INTO v_rmb_onhandnum; EXIT WHEN c_rmb_onhandnum_withnocode%NOTFOUND; v_rmb_onhandnum_nonhandnum := v_rmb_onhandnum.nonhandnum; IF (v_rmb_onhandnum_nonhandnum > 0) --如果有店存量,就檢查對應的現存是否有資料,而且數量是否一致!!! THEN v_rmb_onhandnum_cinvmanid := v_rmb_onhandnum.cinvmanid; v_rmb_onhandnum_cwhid := v_rmb_onhandnum.cwhid; v_rmb_onhandnum_cinvbasid := v_rmb_onhandnum.cinvbasid; v_rmb_onhandnum_vbatchcode := v_rmb_onhandnum.vbatchcode; IF (v_rmb_onhandnum_vbatchcode IS NULL) THEN --如果批次號為空,檢查有無對 應數量 SELECT COUNT (*) INTO num FROM ic_onhandnum WHERE NVL (dr, 0) = 0 AND (nonhandnum <> 0) AND (cinventoryid = v_rmb_onhandnum_cinvmanid) AND (cwarehouseid = v_rmb_onhandnum_cwhid) AND (Cinvbasid = v_rmb_onhandnum_cinvbasid) AND (pk_corp <> '1001'); IF num <> 1 THEN IF num <= 0 THEN i := i + 1; --此處,再次增加查詢,打到店存商品的數量及資訊顯示!!!! SELECT invclassname invclass, invcode, invname, measname measure, num, storname INTO v_product FROM (SELECT cls.invclassname invclassname, inv.invcode invcode, inv.invname invname, meas.measname measname, oh.nonhandnum num, bs.STORNAME storname FROM rmb_onhandnum oh LEFT OUTER JOIN dtjt_invcheck t ON oh.conhandnumid = t.conhandnumid LEFT OUTER JOIN rmb_onhandfreeze f ON oh.conhandnumid = f.conhandnumid LEFT OUTER JOIN bd_invbasdoc inv ON oh.cinvbasid = inv.pk_invbasdoc LEFT OUTER JOIN bd_invcl cls ON inv.pk_invcl = cls.pk_invcl LEFT OUTER JOIN bd_measdoc meas ON inv.pk_measdoc = meas.pk_measdoc LEFT OUTER JOIN bd_invmandoc inm ON inm.PK_INVBASDOC = inv.PK_INVBASDOC LEFT OUTER JOIN bd_stordoc bs ON bs.PK_STORDOC = oh.CWHID WHERE 1 = 1 AND NVL (oh.dr, 0) = 0 AND NVL (t.dr, 0) = 0 AND NVL (cls.dr, 0) = 0 AND NVL (inv.dr, 0) = 0 AND NVL (meas.dr, 0) = 0 AND NVL (inm.dr, 0) = 0 AND NVL (bs.dr, 0) = 0 AND inv.PK_INVBASDOC = v_rmb_onhandnum_cinvbasid AND inm.PK_INVMANDOC = v_rmb_onhandnum_cinvmanid AND oh.nonhandnum > 0 AND (oh.cinvmanid = v_rmb_onhandnum_cinvmanid) AND (oh.cwhid = v_rmb_onhandnum_cwhid) AND (oh.cinvbasid = v_rmb_onhandnum_cinvbasid)); DBMS_OUTPUT.put_line ( '無批次檢查異常結果顯示:' || i || ',店鋪名:' || v_product.storname || ' ,商品名稱:' || v_product.invname || ' ,INVCODE:' || v_product.invcode || ' ,店存數量:' || v_rmb_onhandnum_nonhandnum || ',注意:該商品在現存內查詢不到對應資訊,請確認!!!!!!'); ELSE i := i + 1; --此處,再次增加查詢,打到店存商品的數量及資訊顯示!!!! DBMS_OUTPUT.put_line ( '檢查結果顯示:NoVbatchocde:' || i || ',無批次號!!!!!!,店存數量為:' || v_rmb_onhandnum_nonhandnum || ',倉庫:' || v_rmb_onhandnum_cwhid || ',存貨基本檔案: ' || v_rmb_onhandnum_cinvbasid || ' ,存貨管理ID: ' || v_rmb_onhandnum_cinvmanid || ',在現存內查詢到不是1條資訊,無法判斷,請檢查兩邊資料是否一致!!!!!!'); END IF; ELSE SELECT * INTO v_ic_onhandnum FROM ic_onhandnum WHERE NVL (dr, 0) = 0 AND (nonhandnum <> 0) AND (cinventoryid = v_rmb_onhandnum_cinvmanid) AND (cwarehouseid = v_rmb_onhandnum_cwhid) AND (Cinvbasid = v_rmb_onhandnum_cinvbasid) AND (pk_corp <> '1001'); v_ic_onhandnum_nonhandnum := v_ic_onhandnum.nonhandnum; IF (v_ic_onhandnum_nonhandnum <> v_rmb_onhandnum_nonhandnum) THEN diffvalue := diffvalue + 1; --此處,再次增加查詢,打到店存商品的數量及資訊顯示!!!! SELECT invclassname invclass, invcode, invname, measname measure, num, storname INTO v_product1 FROM (SELECT cls.invclassname invclassname, inv.invcode invcode, inv.invname invname, meas.measname measname, oh.nonhandnum num, bs.STORNAME storname FROM rmb_onhandnum oh LEFT OUTER JOIN dtjt_invcheck t ON oh.conhandnumid = t.conhandnumid LEFT OUTER JOIN rmb_onhandfreeze f ON oh.conhandnumid = f.conhandnumid LEFT OUTER JOIN bd_invbasdoc inv ON oh.cinvbasid = inv.pk_invbasdoc LEFT OUTER JOIN bd_invcl cls ON inv.pk_invcl = cls.pk_invcl LEFT OUTER JOIN bd_measdoc meas ON inv.pk_measdoc = meas.pk_measdoc LEFT OUTER JOIN bd_invmandoc inm ON inm.PK_INVBASDOC = inv.PK_INVBASDOC LEFT OUTER JOIN bd_stordoc bs ON bs.PK_STORDOC = oh.CWHID WHERE 1 = 1 AND NVL (oh.dr, 0) = 0 AND NVL (t.dr, 0) = 0 AND NVL (cls.dr, 0) = 0 AND NVL (inv.dr, 0) = 0 AND NVL (meas.dr, 0) = 0 AND NVL (inm.dr, 0) = 0 AND NVL (bs.dr, 0) = 0 AND inv.PK_INVBASDOC = v_rmb_onhandnum_cinvbasid AND inm.PK_INVMANDOC = v_rmb_onhandnum_cinvmanid AND oh.nonhandnum > 0 AND oh.nonhandnum = v_rmb_onhandnum_nonhandnum AND (oh.cinvmanid = v_rmb_onhandnum_cinvmanid) AND (oh.cwhid = v_rmb_onhandnum_cwhid) AND (oh.cinvbasid = v_rmb_onhandnum_cinvbasid)); DBMS_OUTPUT.put_line ( '無批次數量不一致檢查結果No:' || diffvalue || ',店存量:' || v_ic_onhandnum_nonhandnum || ' ,現存量:' || v_rmb_onhandnum_nonhandnum || ' ,店鋪名:' || v_product1.storname || ' ,商品名稱:' || v_product1.invname || ' ,INVCODE:' || v_product1.invcode || ' ,店存/現存數量不一致,請檢查!!!!!!'); END IF; END IF; ELSE --如果批次號不為空, DBMS_OUTPUT.put_line ( '怎麼搞的,會有批次號呢,檢查SQL語句!!!'); END IF; END IF; END LOOP; IF c_rmb_onhandnum_withnocode%ISOPEN THEN CLOSE c_rmb_onhandnum_withnocode; END IF; num := i + diffvalue; IF (num < 1) THEN DBMS_OUTPUT.put_line ( '以店存量驗證無批次號產品正常!'); ELSE DBMS_OUTPUT.put_line ( '以店存量驗證無批次號產品完畢,錯誤數量:' || num); END IF; i := 0; j := 0; diffvalue := 0; DBMS_OUTPUT.put_line ('==============================================='); DBMS_OUTPUT.put_line ( '============以下查詢有批次號現存/店存資料=========='); DBMS_OUTPUT.put_line ('==============================================='); IF NOT c_rmb_onhandnum_withcode%ISOPEN THEN OPEN c_rmb_onhandnum_withcode; END IF; NULL; LOOP FETCH c_rmb_onhandnum_withcode INTO v_rmb_onhandnum; EXIT WHEN c_rmb_onhandnum_withcode%NOTFOUND; v_rmb_onhandnum_nonhandnum := v_rmb_onhandnum.nonhandnum; IF (v_rmb_onhandnum_nonhandnum > 0) --如果有店存量,就檢查對應的現存是否有資料,而且數量是否一致!!! THEN v_rmb_onhandnum_cinvmanid := v_rmb_onhandnum.cinvmanid; v_rmb_onhandnum_cwhid := v_rmb_onhandnum.cwhid; v_rmb_onhandnum_cinvbasid := v_rmb_onhandnum.cinvbasid; v_rmb_onhandnum_vbatchcode := v_rmb_onhandnum.vbatchcode; IF (v_rmb_onhandnum_vbatchcode IS NULL) THEN --如果批次號為空,報怎麼回事??? DBMS_OUTPUT.put_line ( '============怎麼回事,批次號為空呢??????=========='); /* SELECT COUNT (*) INTO num FROM ic_onhandnum WHERE NVL (dr, 0) = 0 AND (nonhandnum <> 0) AND (cinventoryid = v_rmb_onhandnum_cinvmanid) AND (cwarehouseid = v_rmb_onhandnum_cwhid) AND (Cinvbasid = v_rmb_onhandnum_cinvbasid) AND (vlot = v_rmb_onhandnum_vbatchcode) AND (pk_corp <> '1001'); IF num <> 1 THEN i := i + 1; DBMS_OUTPUT.put_line ( '檢查結果顯示:NoVbatchocde:' || i || ',無批次號!!!!!!,倉庫:' || v_rmb_onhandnum_cwhid || ',存貨基本檔案: ' || v_rmb_onhandnum_cinvbasid || ' ,存貨管理ID: ' || v_rmb_onhandnum_cinvmanid || ',在現存內查詢不到對應資訊或是查詢到不是1條資訊,無法判斷,請檢查兩邊資料是否一致!!!!!!'); ELSE SELECT * INTO v_ic_onhandnum FROM ic_onhandnum WHERE NVL (dr, 0) = 0 AND (nonhandnum <> 0) AND (cinventoryid = v_rmb_onhandnum_cinvmanid) AND (cwarehouseid = v_rmb_onhandnum_cwhid) AND (Cinvbasid = v_rmb_onhandnum_cinvbasid) AND (vlot = v_rmb_onhandnum_vbatchcode) AND (pk_corp <> '1001'); v_ic_onhandnum_nonhandnum := v_ic_onhandnum.nonhandnum; IF (v_ic_onhandnum_nonhandnum <> v_rmb_onhandnum_nonhandnum) THEN diffvalue := diffvalue + 1; DBMS_OUTPUT.put_line ( '檢查結果顯示:No:' || diffvalue || ',批次號!!!!!!' || v_rmb_onhandnum_vbatchcode || ' ,現存量:' || v_ic_onhandnum_nonhandnum || ',店存量: ' || v_rmb_onhandnum_nonhandnum || ' ,倉庫:' || v_rmb_onhandnum_cwhid || ',存貨基本檔案: ' || v_rmb_onhandnum_cinvbasid || ' ,存貨管理ID: ' || v_rmb_onhandnum_cinvmanid || ',在庫存數量不一致,請檢查!!!!!!'); END IF;*/ -- END IF; ELSE --如果批次號不為空,檢查有無對 應數量 SELECT COUNT (*) INTO num FROM ic_onhandnum WHERE NVL (dr, 0) = 0 AND (nonhandnum <> 0) AND (cinventoryid = v_rmb_onhandnum_cinvmanid) AND (cwarehouseid = v_rmb_onhandnum_cwhid) AND (Cinvbasid = v_rmb_onhandnum_cinvbasid) AND (vlot = v_rmb_onhandnum_vbatchcode) AND (pk_corp <> '1001'); IF (num <> 1) THEN IF num <= 0 THEN SELECT invclassname invclass, invcode, invname, measname measure, num, storname INTO v_product2 FROM (SELECT cls.invclassname invclassname, inv.invcode invcode, inv.invname invname, meas.measname measname, oh.nonhandnum num, bs.STORNAME storname FROM rmb_onhandnum oh LEFT OUTER JOIN dtjt_invcheck t ON oh.conhandnumid = t.conhandnumid LEFT OUTER JOIN rmb_onhandfreeze f ON oh.conhandnumid = f.conhandnumid LEFT OUTER JOIN bd_invbasdoc inv ON oh.cinvbasid = inv.pk_invbasdoc LEFT OUTER JOIN bd_invcl cls ON inv.pk_invcl = cls.pk_invcl LEFT OUTER JOIN bd_measdoc meas ON inv.pk_measdoc = meas.pk_measdoc LEFT OUTER JOIN bd_invmandoc inm ON inm.PK_INVBASDOC = inv.PK_INVBASDOC LEFT OUTER JOIN bd_stordoc bs ON bs.PK_STORDOC = oh.CWHID WHERE 1 = 1 AND NVL (oh.dr, 0) = 0 AND NVL (t.dr, 0) = 0 AND NVL (cls.dr, 0) = 0 AND NVL (inv.dr, 0) = 0 AND NVL (meas.dr, 0) = 0 AND NVL (inm.dr, 0) = 0 AND NVL (bs.dr, 0) = 0 AND inv.PK_INVBASDOC = v_rmb_onhandnum_cinvbasid AND inm.PK_INVMANDOC = v_rmb_onhandnum_cinvmanid AND oh.vbatchcode = v_rmb_onhandnum_vbatchcode AND oh.nonhandnum > 0 AND (oh.cinvmanid = v_rmb_onhandnum_cinvmanid) AND (oh.cwhid = v_rmb_onhandnum_cwhid) AND (oh.cinvbasid = v_rmb_onhandnum_cinvbasid)); i := i + 1; DBMS_OUTPUT.put_line ( '檢查結果顯示:No:' || i || ',批次號:' || v_rmb_onhandnum_vbatchcode || ' ,店存數量:' || v_rmb_onhandnum_nonhandnum || ' ,店鋪名稱:' || v_product2.storname || ' ,商品名稱:' || v_product2.invname || ' ,INVCODE:' || v_product2.invcode || ',在現存內查詢不到對應資訊,無法判斷,請檢查兩邊資料是否一致!!!!!!'); ELSE i := i + 1; DBMS_OUTPUT.put_line ( '檢查結果顯示:No:' || i || ',批次號:' || v_rmb_onhandnum_vbatchcode || ',店存數量:' || v_rmb_onhandnum_nonhandnum || ' ,倉庫:' || v_rmb_onhandnum_cwhid || ',存貨基本檔案: ' || v_rmb_onhandnum_Cinvbasid || ' ,存貨管理ID: ' || v_rmb_onhandnum_cinvmanid || ',在現存內查詢到不是1條資訊,無法判斷,請檢查兩邊資料是否一致!!!!!!'); END IF; ELSE SELECT * INTO v_ic_onhandnum FROM ic_onhandnum WHERE NVL (dr, 0) = 0 AND (nonhandnum <> 0) AND (cinventoryid = v_rmb_onhandnum_cinvmanid) AND (cwarehouseid = v_rmb_onhandnum_cwhid) AND (Cinvbasid = v_rmb_onhandnum_cinvbasid) AND (vlot = v_rmb_onhandnum_vbatchcode) AND (vlot = v_rmb_onhandnum_vbatchcode) AND (pk_corp <> '1001'); v_ic_onhandnum_nonhandnum := v_ic_onhandnum.nonhandnum; IF (v_ic_onhandnum_nonhandnum <> v_rmb_onhandnum_nonhandnum) THEN diffvalue := diffvalue + 1; DBMS_OUTPUT.put_line ( '檢查結果顯示:No:' || diffvalue || ',批次號:' || v_rmb_onhandnum_vbatchcode || ' ,現存量:' || v_ic_onhandnum_nonhandnum || ',店存量: ' || v_rmb_onhandnum_nonhandnum || ' ,倉庫:' || v_rmb_onhandnum_cwhid || ',存貨基本檔案: ' || v_rmb_onhandnum_Cinvbasid || ' ,存貨管理ID: ' || v_rmb_onhandnum_cinvmanid || ',在庫存數量不一致,請檢查!!!!!!'); END IF; END IF; END IF; END IF; END LOOP; IF c_rmb_onhandnum_withcode%ISOPEN THEN CLOSE c_rmb_onhandnum_withcode; END IF; num := i + diffvalue; IF (num < 1) THEN DBMS_OUTPUT.put_line ( '以店存量驗證有批次與產品正常!'); ELSE DBMS_OUTPUT.put_line ( '以店存量驗證有批次號產品完畢,錯誤數量:' || num); END IF; END; /