Solved

Cannot delete on inner join

Posted on 2008-11-02
6
885 Views
Last Modified: 2008-12-18
I have a couple of bad records that i need to delete from my customer table and also remove the corresponding address record.
I can return all the rows I need to delete with;

select *      
from       dbo.CUSTOMER inner join            dbo.ADDRESS
on            customer.home_address = address.address_code
where      customer_code      like      '%[^0-9]%'

but changing the query above to

delete *      
from       dbo.CUSTOMER inner join            dbo.ADDRESS
on            customer.home_address = address.address_code
where      customer_code      like      '%[^0-9]%'

does not work not sure what I am missing.
Thanks
0
Comment
Question by:sweetaz
6 Comments
 
LVL 22

Assisted Solution

by:Kelvin Sparks
Kelvin Sparks earned 70 total points
Comment Utility
You cannot delete from customers while there is a corresponding record in address. Either upgrade the table relationships to allows cascade deletes between these two tables or delete the address records first, then the customer records.


Kelvin
0
 
LVL 17

Assisted Solution

by:HuyBD
HuyBD earned 80 total points
Comment Utility
try this
delete dbo.CUSTOMER    

from       dbo.CUSTOMER inner join            dbo.ADDRESS

on            customer.home_address = address.address_code

where      customer_code      like      '%[^0-9]%'

Open in new window

0
 

Author Comment

by:sweetaz
Comment Utility
thanks HuyBD b ut the query you suggest only seems to remove the records from customer table and not the address table.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 59

Accepted Solution

by:
Kevin Cross earned 100 total points
Comment Utility
Kelvin's first comment is correct.  You cannot delete from both.  HuyBD's suggestion helps you with syntax on proper delete using the relationship to do the filtering, so in your case it appears that CUSTOMER table has the key to finding records to delete so you want to do this (unless you enable cascading delete from CUSTOMER to ADDRESS table.
-- delete matching addresses

DELETE a

FROM dbo.ADDRESS a 

INNER JOIN dbo.CUSTOMER c ON c.home_address = a.address_code

WHERE c.customer_code LIKE '%[^0-9]%'
 

-- delete matching customers

DELETE c

FROM dbo.CUSTOMER c

WHERE c.customer_code LIKE '%[^0-9]%'

Open in new window

0
 

Author Comment

by:sweetaz
Comment Utility
thanks mwvisa1 this actually did occur to me lastnight. I should wait 24 hours before commenting! I blame monday-itis.  
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
:)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

772 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

10 Experts available now in Live!

Get 1:1 Help Now