当前位置:网站首页>Synchronization of data create trigger synchronization table for each site

Synchronization of data create trigger synchronization table for each site

2022-07-06 19:50:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm the king of the whole stack .

stay data Mention previous blogs when synchronizing , In every site there will be create triggers for every worksheet , When running CRUD. The task of trigger is to operate it sql The declaration is spliced into a string , And stored in the table synchro_tb_operate_log in , Suppose the trigger runs abnormally , Save its exception information in another table :SYNCHRO_DATA_EXCEP_LOG, among

synchro_tb_operate_log Field information : Primary key ID、 Spliced sql sentence ( Including the primary key ID And region code )、 Whether the synchronization is completed ( Tacit feeling 0 It's not over )、 Creation time

SYNCHRO_DATA_EXCEP_LOG Field information : Primary key ID、 Trigger exception name 、 Trigger exception information 、 The time when the trigger exception occurs

Here's how to create item_rec Code , Let's also learn the syntax and knowledge related to creating triggers :

create or replace
TRIGGER "ADMIN"."TRIGGER_ITEM_REC"
  after insert or update or delete
  on ITEM_REC FOR EACH ROW

/** HEAD
 * @name  Project log table trigger 
 * @description  Change event . formation ddl Statement is inserted into the synchronization table 
 * @version 1.0.0
 * @author Tang
 * @create-date 2014-07-01
 */
declare
  v_sql nvarchar2(3000);             -- To store temporarily sql
  v_zdcode varchar2(30):='08052';    -- Website code 
  v_exception varchar2(500);
begin
  v_sql := null;
  case when inserting then-- insert data 
          v_sql := 'insert into ITEM_REC('||
          'ID,PROTEAMNAME,PY,PROID,'||
          'JCTYPE,WORKFLAG,ZXFLAG,JWDCODE)'||
          'values('''||
          :new.ID||''','''||replace(:new.PROTEAMNAME,'''','')||''','''||:new.PY||''','''||:new.PROID||''','''||
          :new.JCTYPE||''','''||:new.WORKFLAG||''','''||:new.ZXFLAG||''','''||v_jwdcode||''')';
       when updating then-- Update data 
          v_sql := 'update ITEM_REC set '||
                       'PROTEAMNAME='''||replace(:new.PROTEAMNAME,'''','')||''''||
                       ',PY='''||:new.PY||''''||
                       ',PROID='''||:new.PROID||''''||
                       ',JCTYPE='''||:new.JCTYPE||''''||
                       ',WORKFLAG='''||:new.WORKFLAG||''''||
                       ',ZXFLAG='''||:new.ZXFLAG||''''||
                    ' where '||
                       'ID='||:old.ID||' and JWDCODE='''||v_jwdcode||'''';
       when deleting then-- Delete data 
          v_sql := 'delete from ITEM_REC t where t.ID='||:old.ID||' and t.JWDCODE='''||v_jwdcode||'''';
  end case;
  if v_sql is not null then-- hypothesis v_sql Not empty . Then insert the corresponding information into synchro_tb_operate_log In the table 
     insert into
            synchro_tb_operate_log(sd_record_id,sd_record_sql,sd_flag,create_time)
          values(SEQ_SYNCHRO_DATA.NEXTVAL,v_sql,0,to_char(sysdate,'YYYY-MM-DD HH24:MI:SS'));
  end if;
exception
  when others then-- Suppose an exception occurs during operation , Insert the exception information into SYNCHRO_DATA_EXCEP_LOG In the table 
       v_exception := substr(sqlcode||'---'||sqlerrm, 1, 500);
      insert into SYNCHRO_DATA_EXCEP_LOG
             (ID,TRIGGER_NAME,EXCEPTION_INFO,EXCEPTION_TIME)
      values
             (sys_guid(),'Trigger_DICT_PROTEAM',v_exception,to_char(sysdate,'YYYY-MM-DD HH24:MI:SS'));
end TRIGGER_ITEM_REC;

Copyright notice : This article is an original blog article , Blog , Without consent , Shall not be reproduced .

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117142.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061146428767.html