Solved

How do I add new rows to a new table where there isn't an existing row that matches the foreign key?

Posted on 2008-10-03
4
183 Views
Last Modified: 2010-04-21
I want to create records in a table with a value for every matching record in another table  but only if there isn't already a record.
So for the following :
Customers                  Locations      
CustomerID                  CustomerID      Location
1                  2      Unknown
2                  3      England
3                  4      Spain
4                        
5                        

I would like for the sql command to end up with the tables looking like this:
Customers                  Locations      
CustomerID                  CustomerID      Location
1                  1      Unknown
2                  2      Unknown
3                  3      England
4                  4      Spain
5                  5      Unknown
0
Comment
Question by:dgloveruk
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
UnifiedIS earned 250 total points
Comment Utility
I'm not following your table layouts but a general way to do this would be:
insert into tableA
(SELECT columns from tableB
WHERE key not in (SElect key from tableA))
0
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 250 total points
Comment Utility
Something like this.  You can replace with actual columns/values you want.
INSERT INTO Locations (CustomerID, Location)

SELECT CustomerID, 'Unknown'

FROM Customers

WHERE CustomerID NOT IN (SELECT CustomerID FROM Locations)
 

-- OR

INSERT INTO Locations (CustomerID, Location)

SELECT CustomerID, 'Unknown'

FROM Customers c

WHERE NOT EXISTS (SELECT 1 FROM Locations l WHERE l.CustomerID = c.CustomerID)

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
Guess, we had the same thought, UnifiedIS. :)
0
 

Author Closing Comment

by:dgloveruk
Comment Utility
Thanks guys, I split the points on this occasion because you actually seemed to answer simultaneously!
Thanks again,
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now