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 2000 Forums
 SQL Server Development (2000)
 Add records to table that don't already exist?

Author  Topic 

Vassago
Starting Member

33 Posts

Posted - 2006-03-20 : 19:53:59
I have two tables that look like this:

table1
id contact_list_name
904 ALL90
905 ROLL30
914 ALL30
915 ALL60

table2
id contact_list_name
905 ROLL30
914 ALL30
915 ALL60
917 ALL90

I would like append all records from table2 to table1 that don't already exist, like this:

id contact_list_name
904 ALL90
905 ROLL30
914 ALL30
915 ALL60
917 ALL90

I have been beating myself up over how to tell if the single combination already exists on the table so sql server can ignore it and move on to the next record. Any recommendations?

Vassago

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-03-20 : 19:59:05
use not exists

declare @id int
insert into table1 (id)
select @id
where not exists (select * from table1 x where x.id = @id)




KH

Choice is an illusion, created between those with power, and those without.
Concordantly, while your first question may be the most pertinent, you may or may not realize it is also the most irrelevant

Go to Top of Page

Vassago
Starting Member

33 Posts

Posted - 2006-03-20 : 20:11:21
Wow, that was quick. Works perfectly! Thanks!
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-03-20 : 20:23:56
to insert table1 with data from table2
insert into table1 (id, contact_list_name)
select id, contact_list_name
from table2 t2
where not exists (select * from table1 x where x.id = t2.id)




KH

Choice is an illusion, created between those with power, and those without.
Concordantly, while your first question may be the most pertinent, you may or may not realize it is also the most irrelevant

Go to Top of Page
   

- Advertisement -