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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Insert records from A to B only records not in B

Author  Topic 

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2009-02-13 : 15:17:25
How to code to insert records from table A to B only records not in B (I need to make OrderID unique)?
I used code below. Is it ok?

insert into B select * from A where OrderID not in (select OrderID from B)

mfemenel
Professor Frink

1421 Posts

Posted - 2009-02-13 : 15:21:19
That will work. If it's a large amount of rows, you may see better performance by doing a left join and selecting rows where b.orderid is null. If it's a small amount of rows though, you're good.

Mike
"oh, that monkey is going to pay"
Go to Top of Page

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2009-02-13 : 15:28:29
Mike,
Can you help me to complete code usiing b.orderid is null?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-02-13 : 15:33:08
insert into B (...)
select ...
from A
where not exists (select * from B where A.OrderID = B.OrderID)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

mfemenel
Professor Frink

1421 Posts

Posted - 2009-02-13 : 15:42:55
Create table #a(orderid int not null)
create table #b(orderid int not null)

insert into #a values(1)
insert into #a values(2)
insert into #b values(1)
select * from #a
select * from #b

insert into b
select a.OrderID
from #a a
left join #b b
on a.orderid=b.orderid
where b.orderid is null

Mike
"oh, that monkey is going to pay"
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-13 : 15:47:40
quote:
Originally posted by Sun Foster

How to code to insert records from table A to B only records not in B (I need to make OrderID unique)?
I used code below. Is it ok?

insert into B select * from A where OrderID not in (select OrderID from B)



This won't Work. See here Why?
http://weblogs.sqlteam.com/mladenp/archive/2007/05/18/60210.aspx
Go to Top of Page

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2009-03-05 : 11:14:58
I tested the way from Tara Kizer.
It only works when B not empty.
When B was truncate, it did not work.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-03-05 : 13:06:27
That doesn't make sense. Show us the actual code you ran.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2009-03-05 : 15:19:54
Here is the code and tables. If you run code, 5 rows will insert into B. (should be only 3 rows)

insert into B select * from A
where not exists (select * from B where A.OrderID = B.OrderID)

table A

OrderID Name

1 AAA
2 BBB
3 CCC
1 AAA
1 AAA
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-03-05 : 16:02:16
Well you'd need to add DISTINCT or a GROUP BY. You hadn't provided sample data before and since we can't read minds, we did our best with the info provided.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -