简体   繁体   中英

How to transfer delta records from one table to another in oracle

I have one table t1 with attributes

emp_joined_date,
emp_name,
emp_number.

and I want to transfer this table to another table t2(having same structure) .

during 1st attempt I will transfer whole table but later I want to transfer only delta records from table t1 to t2 .

So how do I insert multiple records from table t1 to t2 in single query(in oracle)

What about this:

insert into t1
select * from t2
where emp_number not in 
(select t2.emp_number from t2 join t1 on t1.emp_number = t2.emp_number) ;

If you get new records in t1 and you want to insert only those in t2 , then use this. It will give you new emp_number

insert into t2 (emp_joined_date,emp_name,emp_number)
select t1.emp_joined_date,t1.emp_name,t1.emp_number 
from t1 left join t2
on t1.emp_number=t2.emp_number
where t2.emp_number is null

If you want to insert records based on time, then use this Use NVL function so for the first run, it would copy everything.

insert into t2 (emp_joined_date,emp_name,emp_number)
select emp_joined_date,emp_name,emp_number from t1 
where emp_joined_date > 
nvl((select max(emp_joined_date) from t1),to_date('1900-01-01','YYYY-MM-DD'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM