Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 transferring only new records from table1 to tab2

Author  Topic 

pnasz
Posting Yak Master

101 Posts

Posted - 2014-06-26 : 02:00:32
I have a table table1

ID Date In Out

22 01/01/2014 10:00 11:00
22 02/01/2014 10:00 11:00
22 03/01/2014 10:00 11:00
23 01/01/2014 10:00 11:00
23 02/01/2014 10:00 11:00
23 03/01/2014 10:00 11:00
24 01/01/2014 10:00 11:00
24 02/01/2014 10:00 11:00
24 03/01/2014 10:00 11:00
25 01/01/2014 10:00 11:00
25 02/01/2014 10:00 11:00
25 03/01/2014 10:00 11:00

Table 2

ID Date In Out

22 01/01/2014 10:00 11:00
22 02/01/2014 10:00 11:00
22 03/01/2014 10:00 11:00
23 01/01/2014 10:00 11:00
23 02/01/2014 10:00 11:00
23 03/01/2014 10:00 11:00
24 01/01/2014 10:00 11:00
24 02/01/2014 10:00 11:00
24 03/01/2014 10:00 11:00

I want to insert only those record from table 1 to table 2 which is not there in table 2

please help

ahmeds08
Aged Yak Warrior

737 Posts

Posted - 2014-06-26 : 02:16:53
can you try this

insert into table2
select id,dateinout from table1
where id not in(select id from table1)

Javeed Ahmed
Go to Top of Page

pnasz
Posting Yak Master

101 Posts

Posted - 2014-06-26 : 02:29:24
what if the situation is like this if id 25 is already there in table 2 and i want to insert record for id 25 date 04,05,06. No field is unique here.

ID Date In Out

22 01/01/2014 10:00 11:00
22 02/01/2014 10:00 11:00
22 03/01/2014 10:00 11:00
23 01/01/2014 10:00 11:00
23 02/01/2014 10:00 11:00
23 03/01/2014 10:00 11:00
24 01/01/2014 10:00 11:00
24 02/01/2014 10:00 11:00
24 03/01/2014 10:00 11:00
25 01/01/2014 10:00 11:00
25 02/01/2014 10:00 11:00
25 03/01/2014 10:00 11:00
25 04/01/2014 10:00 11:00
25 05/01/2014 10:00 11:00
25 06/01/2014 10:00 11:00

Table 2

ID Date In Out

22 01/01/2014 10:00 11:00
22 02/01/2014 10:00 11:00
22 03/01/2014 10:00 11:00
23 01/01/2014 10:00 11:00
23 02/01/2014 10:00 11:00
23 03/01/2014 10:00 11:00
24 01/01/2014 10:00 11:00
24 02/01/2014 10:00 11:00
24 03/01/2014 10:00 11:00
25 01/01/2014 10:00 11:00
25 02/01/2014 10:00 11:00
25 03/01/2014 10:00 11:00
Go to Top of Page

MariusC
Starting Member

16 Posts

Posted - 2014-06-26 : 03:36:29
You can try this with the MERGE query.

Hope this will help:
http://www.codeproject.com/Articles/37172/Merge-Statement-in-SQL-Server
Go to Top of Page
   

- Advertisement -