zoukankan      html  css  js  c++  java
  • oracle存储过程,增量同步处理数据、根据id新增或更新

    比如同步供应商数据:

    CREATE TABLE jd_unit(
        id varchar2(64) primary key,
        unit_name varchar2(500),
        unit_code varchar2(500),
        is_enable char(1),
        create_time timestamp,
        update_time timestamp,
        sync_time timestamp
    );
    comment on table jd_unit is '供应商表';
    comment on column jd_unit.id is '主键';
    comment on column jd_unit.unit_name is '供应商名称';
    comment on column jd_unit.unit_code is '供应商编码';
    comment on column jd_unit.is_enable is '是否可用:1=可用;0=不可用';
    comment on column jd_unit.create_time is '创建时间';
    comment on column jd_unit.update_time is '更新时间';
    comment on column jd_unit.sync_time is '同步时间';

    同步处理的记录时间表:

    CREATE TABLE jd_sync_handle(
        id varchar2(64) primary key,
        sync_time timestamp,
        handle_time timestamp
    );
    comment on table jd_sync_handle is '数据处理表';
    comment on column jd_sync_handle.id is '主键,固定取值jd_unit、jd_unit_contract';
    comment on column jd_sync_handle.sync_time is '上次处理的表中最大同步时间';
    comment on column jd_sync_handle.handle_time is '上次处理的结束处理时间';

    处理表初始化:

    INSERT INTO JD_SYNC_HANDLE(ID, SYNC_TIME, HANDLE_TIME) VALUES ('jd_unit', TO_TIMESTAMP('2020-11-20 16:12:43.000000', 'SYYYY-MM-DD HH24:MI:SS:FF6'), TO_TIMESTAMP('2020-11-20 16:12:50.000000', 'SYYYY-MM-DD HH24:MI:SS:FF6'));

    增量同步处理数据、根据id新增或更新,存储过程:

    CREATE OR REPLACE PROCEDURE jd_unit_handle AS
    -- 变量定义在begin在前
    tmp_id jd_unit.id%TYPE;
    tmp_is_enable jd_unit.is_enable%TYPE;
    tmp_is_delete varchar2(1);
    tmp_create_time jd_unit.create_time%TYPE;
    tmp_update_time jd_unit.update_time%TYPE;
    tmp_unit_name jd_unit.unit_name%TYPE;
    tmp_unit_code jd_unit.unit_code%TYPE;
    tmp_sync_time jd_unit.sync_time%TYPE;
    
    handle_sync_time JD_SYNC_HANDLE.sync_time%TYPE;
    dbDataCnt int;
    
        
    CURSOR emp_cursor is select DISTINCT id,is_enable,case is_enable when '1' then '0' else '1' end,create_time,update_time,unit_name,unit_code,sync_time
        from jd_unit where sync_time>(select sync_time from JD_SYNC_HANDLE where id='jd_unit');
    
    BEGIN
        select sync_time into handle_sync_time from JD_SYNC_HANDLE where id='jd_unit';
        dbms_output.put_line('last handle_sync_time:'||handle_sync_time);
        --循环开始
        LOOP
        dbms_output.put_line('LOOP');
    
      IF NOT emp_cursor%ISOPEN  THEN
         OPEN emp_cursor;
      END IF; 
      
      FETCH emp_cursor INTO  tmp_id,tmp_is_enable,tmp_is_delete,tmp_create_time,tmp_update_time,tmp_unit_name,tmp_unit_code,tmp_sync_time;
        
        dbms_output.put_line('FETCH-->id:'||tmp_id);
        
        if tmp_id!='exit' then 
                dbms_output.put_line('handle data');
                -- 处理同步时间:取查找的数据最大时间
                if tmp_sync_time>handle_sync_time then 
                    handle_sync_time:=tmp_sync_time;
                    dbms_output.put_line('handle_sync_time change:'||handle_sync_time);
                else dbms_output.put_line('handle_sync_time no change');
                end if;
                -- 处理数据
                -- 查询该id是否存在表中
                select count(1) into dbDataCnt from yf_unit_type where id=tmp_id;
                dbms_output.put_line('dbDataCnt:'||dbDataCnt);
                -- 判断是否存在该数据
                if dbDataCnt=0 then
                    insert into YF_UNIT_TYPE(ID,IS_ENABLE,IS_DELETE,CREATE_TIME,CREATE_USER_ID,UPDATE_TIME,UPDATE_USER_ID,UNIT_NAME,UNIT_CODE,UNIT_TYPE)
          values(tmp_id,tmp_is_enable,tmp_is_delete,tmp_create_time,'1',tmp_update_time,'1',tmp_unit_name,tmp_unit_code,'a,b');
                else 
                        update YF_UNIT_TYPE set IS_DELETE=tmp_is_delete,IS_ENABLE=tmp_is_enable,CREATE_TIME=tmp_create_time,UPDATE_TIME=tmp_update_time,UNIT_NAME=tmp_unit_name,UNIT_CODE=tmp_unit_code
                    where id=tmp_id;
                end if;
                -- 重置下是否存在表中
                dbDataCnt:=0;
        else dbms_output.put_line('no data to handle');
      end if;
        
        
      --退出循环的条件
      EXIT WHEN emp_cursor%NOTFOUND OR emp_cursor%NOTFOUND IS NULL;
        
        -- 退出设置id为值为exit
        tmp_id:='exit';
        
        END LOOP;
        dbms_output.put_line('END LOOP');
        
        -- 更新last handle_sync_time
        update JD_SYNC_HANDLE set sync_time=handle_sync_time,handle_time=sysdate where id='jd_unit';
    END;
  • 相关阅读:
    GIS开发 坐标系问题
    IE不支持option的display样式,只能使用remove和add【转】
    使用jQuery解决IE不支持的option disable属性【转】
    mount 时遇到connection unfused 错误操作
    调试onvif 用到的一些工具。。linux 命令!
    jQuery实现删除option控件下的元素
    去掉input 输入框的边框
    Json文件读取
    JQuery 改指定的内容 设置隐藏内容为可见
    Ubuntu 下静态IP修改
  • 原文地址:https://www.cnblogs.com/xmqa/p/14046668.html
Copyright © 2011-2022 走看看