首页 > 代码库 > oracle简单存储过程的编写

oracle简单存储过程的编写

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;
    -- 查询中间表
    SELECT COUNT(*) INTO V_MID_COUNT  FROM tt_trms_car_task_mid M WHERE  M.TASK_ID = RX.TASK_ID AND M.POSITION_NO = RX.POSITION_NO AND M.ZONE_CODE = RX.ZONE_CODE ;
    IF V_MID_COUNT = 0 THEN
        -- mid表新增记录
        INSERT INTO TT_TRMS_CAR_TASK_MID
          (ID,
           ORIGINAL_ID,
           SEND_CAR_TM,
           REQUIRE_ID,
           TASK_ID,
           POSITION_NO,
           LINE_CODE,
           BATCH_CODE,
           PLAN_START_TM,
           ZONE_CODE,
           PLATE_NUM,
           DEL_FLAG,
           dest_code,
           stowage_city,
           DEAL_TM,
           DEAL_FLG,
           DEAL_IP,
           DEAL_COUNT,
           INSERT_TM,
           DELIVE_CODE)
        VALUES
          (SEQ_TT_TRMS_CAR_TASK_MID.NEXTVAL,
           RX.ORIGINAL_ID,
           RX.SEND_CAR_TM,
           RX.REQUIRE_ID,
           RX.TASK_ID,
           RX.POSITION_NO,
           RX.LINE_CODE,
           RX.BATCH_CODE,
           RX.PLAN_START_TM,
           RX.ZONE_CODE,
           RX.PLATE_NUM,
           RX.DEL_FLAG,
           RX.dest_code,
           RX.stowage_city,
           RX.DEAL_TM,
           RX.DEAL_FLG,
           RX.DEAL_IP,
           RX.DEAL_COUNT,
           CURRENT_TIMESTAMP,
           RX.DELIVE_CODE);
            -- dist表新增记录
        INSERT INTO TT_TRMS_CAR_TASK_DIST
          (ID,
           ORIGINAL_ID,
           SEND_CAR_TM,
           REQUIRE_ID,
           TASK_ID,
           POSITION_NO,
           LINE_CODE,
           BATCH_CODE,
           PLAN_START_TM,
           ZONE_CODE,
           PLATE_NUM,
           DEL_FLAG,
           dest_code,
           stowage_city,
           DEAL_TM,
           DEAL_FLG,
           DEAL_IP,
           DEAL_COUNT,
           INSERT_TM,
           DELIVE_CODE)
        VALUES
          (SEQ_TT_TRMS_CAR_TASK_DIST.NEXTVAL,
           RX.ORIGINAL_ID,
           RX.SEND_CAR_TM,
           RX.REQUIRE_ID,
           RX.TASK_ID,
           RX.POSITION_NO,
           RX.LINE_CODE,
           RX.BATCH_CODE,
           RX.PLAN_START_TM,
           RX.ZONE_CODE,
           RX.PLATE_NUM,
           RX.DEL_FLAG,
           RX.dest_code,
           RX.stowage_city,
           RX.DEAL_TM,
           RX.DEAL_FLG,
           RX.DEAL_IP,
           RX.DEAL_COUNT,
           CURRENT_TIMESTAMP,
           RX.DELIVE_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.POSITION_NO = RX.POSITION_NO AND M.ZONE_CODE = RX.ZONE_CODE AND ROWNUM=1 ) LOOP
           IF RM.ORIGINAL_ID < RX.ORIGINAL_ID THEN
             -- 版本号>当前版本号 mid表更新
              UPDATE TT_TRMS_CAR_TASK_MID
                  SET
                      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,
                      dest_code     = RX.dest_code,
                      stowage_city  = RX.stowage_city,
                      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;
               -- dist表新增记录
              INSERT INTO TT_TRMS_CAR_TASK_DIST
                (ID,
                 ORIGINAL_ID,
                 SEND_CAR_TM,
                 REQUIRE_ID,
                 TASK_ID,
                 POSITION_NO,
                 LINE_CODE,
                 BATCH_CODE,
                 PLAN_START_TM,
                 ZONE_CODE,
                 PLATE_NUM,
                 DEL_FLAG,
                 dest_code,
                 stowage_city,
                 DEAL_TM,
                 DEAL_FLG,
                 DEAL_IP,
                 DEAL_COUNT,
                 INSERT_TM,
                 DELIVE_CODE)
              VALUES
                (SEQ_TT_TRMS_CAR_TASK_DIST.NEXTVAL,
                 RX.ORIGINAL_ID,
                 RX.SEND_CAR_TM,
                 RX.REQUIRE_ID,
                 RX.TASK_ID,
                 RX.POSITION_NO,
                 RX.LINE_CODE,
                 RX.BATCH_CODE,
                 RX.PLAN_START_TM,
                 RX.ZONE_CODE,
                 RX.PLATE_NUM,
                 RX.DEL_FLAG,
                 RX.dest_code,
                 RX.stowage_city,
                 RX.DEAL_TM,
                 RX.DEAL_FLG,
                 RX.DEAL_IP,
                 RX.DEAL_COUNT,
                 CURRENT_TIMESTAMP,
                 RX.DELIVE_CODE);
           END IF;
        END LOOP;
    END IF;

        -- 更新tt_trms_car_task表数据状态
    UPDATE tt_trms_car_task SET DEAL_FLG = 2 WHERE ID = RX.id;

    --批次提交控制
    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;

  

oracle简单存储过程的编写