Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Cannot delete on inner join

Posted on 2008-11-02
6
Medium Priority
?
897 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 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.


Kelvin
0
 
LVL 17

Assisted Solution

by:HuyBD
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

0
 

Author Comment

by:sweetaz
ID: 22864528
thanks HuyBD b ut the query you suggest only seems to remove the records from customer table and not the address table.
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 61

Accepted Solution

by:
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
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
ID: 22871747
thanks mwvisa1 this actually did occur to me lastnight. I should wait 24 hours before commenting! I blame monday-itis.  
0
 
LVL 61

Expert Comment

by:Kevin Cross
ID: 22872919
:)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

580 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