Solved

SQL task for adding data not in table

Posted on 2011-09-20
6
239 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

839 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