Solved

SQL task for adding data not in table

Posted on 2011-09-20
6
240 Views
Last Modified: 2012-05-12
Hi all.

I have a SQL 2000 DTS that I would like to insert data present in one table not in a second table.

The first table (myTableA) has one field: ItemCategory

The second table (myTableB) has 2 fields: CustomerID and ItemCategory

I want to be able to add values from the first table (myTableA) that are not present in myTableB.

For example:

myTableA: AAA, ABC, DEF
myTableB: CustomerID: 123, ItemCategory: AAA
CustomerID: 123, ItemCategory: ABC

So the task would run and find that CustomerID: 123 was missing ItemCategory DEF

Any idea how I can do this?

Thank you in advance!
0
Comment
Question by:printmedia
  • 3
  • 3
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36567369
for a single customer:
select * from mytableA a
where not exists( select null from myTableB b where b.customerid = 123 and b.itemcategory = a.itemcategory ) 

Open in new window



0
 

Author Comment

by:printmedia
ID: 36567382
How about for mutlitple customers?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36567399
supposing you have a table with the customers:
select c.customerid, a.itemcategory
from mytableA a
cross join tblCustomer c
where not exists( select null from myTableB b where b.customerid = c.customerid and b.itemcategory = a.itemcategory )

Open in new window

0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:printmedia
ID: 36567431
Although we do have a separate customer table, the customers in myTableB are specific customers that do not include all our customers. The table that will hold the customers is myTableB.

The SQL task that runs before this one, adds new customers to myTableB so by the time this task runs, myTableB will have an updated customer list.

This task's purpose is to add new itemcategories (a new itemcategory is added to myTableA) to the existing list of customers in myTableB.

0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 36567459
we can use the same logic, nevertheless:
select c.customerid, a.itemcategory
from mytableA a
cross join (select customerid from mytableB group by customerid ) c
where not exists( select null from myTableB b where b.customerid = c.customerid and b.itemcategory = a.itemcategory )

Open in new window

0
 

Author Closing Comment

by:printmedia
ID: 36567491
Great thanks!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

680 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