Solved

In SQL, for each value returned how do I delete rows in another table identified by each returned values?

Posted on 2009-05-13
4
176 Views
Last Modified: 2012-05-06
In SQL, for each value returned how do I delete rows in another table identified by each returned values

Consider, I have table A:

key | name_key
1       matt d.
2       keith r.
3       bob h.

In table B I have

name_key | cars
1       koenigsegg
1       veyron
1       enzo
2       astra
3       veyron

Basically, I want the command to delete the owners of veyrons from table A (1 and 3) as well as the veyron entry in table B.
However there should be enough logic to know not to remove entry 1 from table A because he owns other cars.

I'm thinking something along
0
Comment
Question by:Matt_Du
4 Comments
 
LVL 4

Accepted Solution

by:
j_s_kelley earned 300 total points
ID: 24380669
Your request doesn't make sense.  If you want to delete the owners of Veyrons in Table A, that would include owner 1.  You can't delete him and leave him in.  I'm gonna take a shot that what you mean is delete that Veyron's and if there are any users that no longer have cars in table B, remove them as well.  You can do this in 2 steps.  See the code.  If this is not what you want to do, please clarify.
Delete From B where Cars = 'Veyron'

Delete From A where key not in (Select name_key from B)

Open in new window

0
 
LVL 25

Assisted Solution

by:reb73
reb73 earned 100 total points
ID: 24380699
Try the sample code below (keep a backup of your original tables just in case!) -

 
DELETE T1

FROM TableA T1

WHERE EXISTS (SELECT NULL FROM  TableB WHERE TableB.name_key = T1.name_key AND cars = 'veyron')

AND NOT EXISTS (SELECT NULL FROM TableA T2 WHERE T2.name_key = T1.name_key AND cars <> 'veyron')
 

DELETE TableB

WHERE cars = 'veyron'

Open in new window

0
 
LVL 57

Assisted Solution

by:Raja Jegan R
Raja Jegan R earned 100 total points
ID: 24380706
Hope this helps:
declare @cnt int,

@key int,

@cars varchar(100);

declare cursor del_cur for

select name_key,cars

from tableB

where cars = 'veyron';
 

OPEN del_cur;
 

FETCH NEXT FROM del_cur

INTO @key, @cars;
 

WHILE @@FETCH_STATUS = 0

BEGIN

   set @cnt = (select count(*) from tableB where name_key = @key and cars = @cars);

   IF @cnt = 1

   begin

      delete from tableB where name_key = @key and cars = @cars;

      delete from tableA where key = @key;

   end

   ELSE

   begin

      delete from tableB where name_key = @key and cars = @cars;

   end
 

FETCH NEXT FROM del_cur

INTO @key, @cars;

END 

CLOSE del_cur

DEALLOCATE del_cur

Open in new window

0
 

Author Closing Comment

by:Matt_Du
ID: 31581266
Thanks all!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
STDEVP in SQL 2 54
sQL pivot 9 46
Convert char to decimal in a SQL Server View 14 42
Help Parsing a String with SQL Syntax 23 15
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…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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

13 Experts available now in Live!

Get 1:1 Help Now