truncate table CUST;truncate table TAG_CUST; truncate table vt_inc;truncate table vt_new; insert into CUST values(100,'張三','13333333333'); insert into CUST values(200,'李四','12222222222'); insert into CUST values(300,'王五','15555555555'); insert into vt_new select CUST_ID,CUST_NAME,PHONE_NUM,'20160101','29991231' from CUST; insert into vt_inc select a.* from vt_new a left join TAG_CUST b on a.CUST_ID=B.CUST_ID and b.ETL_END_DT='29991231' where b.CUST_ID is null or a.PHONE_NUM<>b.PHONE_NUM; insert into vt_inc select b.CUST_ID,a.CUST_NAME,a.PHONE_NUM,a.ETL_START_DT,'19990101' from vt_new a right join TAG_CUST b on a.CUST_ID=b.CUST_ID where a.CUST_ID is null and b.ETL_END_DT='29991231'; update TAG_CUST a set ETL_END_DT='20160102' where ETL_END_DT='29991231' and exists (select 1 from vt_inc b where a.CUST_ID=b.CUST_ID); insert into TAG_CUST select a.* from vt_inc a where a.ETL_END_DT='29991231';