Solved

ASP.NET query to delete

Posted on 2012-04-10
2
340 Views
Last Modified: 2012-06-22
I need a query that deletes everything from the "Names" table but only where where is not a matching ID in the "Departments" table.  So in the example below, ID's 1, 3 and 6 would  be deleted, but ID's 2, 4 and 5 would not be deleted.  I can't figure out the query.

   Names                                                    Departments

ID     Name                                           ID            Department
==     =====                                          ==            ==========
1       Smith                                          2               Accounting
2       Jones                                           4               Management
3       Miller                                          5               Purchasing
4       Washington
5       Lewis
6       Brown
0
Comment
Question by:zintech
2 Comments
 
LVL 15

Expert Comment

by:Anuj
ID: 37828225
DELETE FROM Name
FROM Names N
INNER JOIN Department M
ON M.ID = N.ID
WHERE N.ID IS NULL
0
 
LVL 5

Accepted Solution

by:
hafeezmca earned 500 total points
ID: 37828340
Hi try this:

delete from names where Id not in (select e.Id from Names e, Departmentt d where e.Id=d.Id)
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

815 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

8 Experts available now in Live!

Get 1:1 Help Now