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
189 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
ID: 22634044
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
ID: 22634050
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
ID: 22634065
Guess, we had the same thought, UnifiedIS. :)
0
 

Author Closing Comment

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 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