Cannot delete on inner join

Posted on 2008-11-02
Medium Priority
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.
Question by:sweetaz
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 22

Assisted Solution

by:Kelvin Sparks
Kelvin Sparks earned 210 total points
ID: 22864206
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.

LVL 17

Assisted Solution

HuyBD earned 240 total points
ID: 22864392
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


Author Comment

ID: 22864528
thanks HuyBD b ut the query you suggest only seems to remove the records from customer table and not the address table.
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

LVL 60

Accepted Solution

Kevin Cross earned 300 total points
ID: 22866336
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
INNER JOIN dbo.CUSTOMER c ON c.home_address = a.address_code
WHERE c.customer_code LIKE '%[^0-9]%'
-- delete matching customers
WHERE c.customer_code LIKE '%[^0-9]%'

Open in new window


Author Comment

ID: 22871747
thanks mwvisa1 this actually did occur to me lastnight. I should wait 24 hours before commenting! I blame monday-itis.  
LVL 60

Expert Comment

by:Kevin Cross
ID: 22872919

Featured Post

Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

764 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