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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 transferring only new records from table1 to tab2
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

pnasz
Posting Yak Master

101 Posts

Posted - 06/26/2014 :  02:00:32  Show Profile  Reply with Quote
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

India
737 Posts

Posted - 06/26/2014 :  02:16:53  Show Profile  Send ahmeds08 a Yahoo! Message  Reply with Quote
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 - 06/26/2014 :  02:29:24  Show Profile  Reply with Quote
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

Romania
16 Posts

Posted - 06/26/2014 :  03:36:29  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000