首页 > 代码库 > ABC表中转数据逻辑

ABC表中转数据逻辑

CREATE OR REPLACE PROCEDURE PROC_TT_TRMS_CAR_TASK
(
    P_BATCH_NUM     IN  NUMBER  DEFAULT 5000,   --处理数据量
    P_COMMIT_NUM    IN  NUMBER  DEFAULT 500     --批次提交数据量
) AS

    V_COUNT         INT DEFAULT 0;  -- 批量提交计数
    V_MID_COUNT     INT DEFAULT 0;  -- 中间表计数

/*
    CREATE DATE:2017-06-18
    CREATE BY:01369076
    DESC  :TT_TRMS_CAR_TASK 批量提取数据,中间表TT_TRMS_CAR_TASK_MID 用于比较 ,下发中转场表TT_TRMS_CAR_TASK_DIST   

*/

BEGIN
    -- 批量提取 
    FOR RX IN ( SELECT T.*,ROWID  as rid  FROM tt_trms_car_task T WHERE T.DEAL_FLG = 0 AND ROWNUM <= P_BATCH_NUM )  LOOP
            
       V_MID_COUNT := 0; -- 初始化MID表记录数
       
       -- 查询中间表
       SELECT COUNT(*) INTO V_MID_COUNT  FROM tt_trms_car_task_mid M WHERE  M.TASK_ID = RX.TASK_ID AND M.BATCH_CODE = RX.BATCH_CODE AND M.Zone_Code = RX.ZONE_CODE ;
       IF V_MID_COUNT = 0 THEN -- mid表新增记录
         INSERT INTO tt_trms_car_task_mid SELECT * FROM  tt_trms_car_task t WHERE t.TASK_ID = RX.TASK_ID AND t.BATCH_CODE = RX.BATCH_CODE AND t.Zone_Code = RX.ZONE_CODE ;
       ELSE -- 对比版本号,修改
         FOR RM IN (SELECT M.* ,ROWID AS rid FROM tt_trms_car_task_mid M WHERE M.TASK_ID = RX.TASK_ID AND M.BATCH_CODE = RX.BATCH_CODE AND M.Zone_Code = RX.ZONE_CODE AND ROWNUM=1 ) LOOP
             IF RM.ORIGINAL_ID < RX.ORIGINAL_ID THEN
                UPDATE tt_trms_car_task_mid
                SET  ID = v_id,
                     original_id = RX.original_id,
                     send_car_tm = RX.send_car_tm,
                     require_id = RX.require_id,
                     task_id = RX.task_id,
                     position_no = RX.position_no,
                     line_code = RX.line_code,
                     batch_code = RX.batch_code,
                     plan_start_tm = RX.plan_start_tm,
                     zone_code = RX.zone_code,
                     plate_num = RX.plate_num,
                     del_flag = RX.del_flag,
                     send_tm = RX.send_tm,
                     deal_tm = RX.deal_tm,
                     deal_flg = RX.deal_flg,
                     deal_ip = RX.deal_ip,
                     deal_count = RX.deal_count,
                     insert_tm = RX.insert_tm,
                     delive_code = RX.delive_code
               WHERE ID = RM.Id;
             END IF
         END LOOP;
         
         -- dist表新增记录
         INSERT INTO tt_trms_car_task_dist SELECT * FROM  tt_trms_car_task t WHERE t.TASK_ID = RX.TASK_ID AND t.BATCH_CODE = RX.BATCH_CODE AND t.Zone_Code = RX.ZONE_CODE ;
         
       END IF;
       
        --批次提交控制
        V_COUNT:=V_COUNT+1 ;
        IF(MOD(V_COUNT,P_COMMIT_NUM)=0)THEN
            COMMIT;
        END IF;
     
    END LOOP;
    COMMIT;
    
EXCEPTION
    WHEN OTHERS THEN
        ROLLBACK;
        PKG_SYS_LOG.ERROR_LOG(NULL,
            ‘PROC_TT_TRMS_CAR_TASK‘,
            SYSDATE,
            SQLCODE,
            SQLERRM,
            DBMS_UTILITY.FORMAT_ERROR_BACKTRACE,NULL
        );
END PROC_TT_TRMS_CAR_TASK;

  

ABC表中转数据逻辑