We help IT Professionals succeed at work.

Need help with Access query to delete records

Gerhardpet
Gerhardpet asked
on
297 Views
Last Modified: 2012-06-27
I have 2 tables in Access and in Table1 I want to find records without matching records in Table2 and then delete them from Table1.

Below I have a query that will find records in ADDRESS table without matching records in LOCAL_ADDRESS table. Now all I need to know how I could get the same query to delete unmatched records in the ADDRESS table.

Can anyone help me with this?

SELECT ADDRESS.RECORD_TYPE, ADDRESS.CEV_NO, ADDRESS.ADDR_TYPE, ADDRESS.ID, ADDRESS.NAME
FROM ADDRESS LEFT JOIN LOCAL_ADDRESS ON (ADDRESS.ID = LOCAL_ADDRESS.ID) AND (ADDRESS.ADDR_TYPE = LOCAL_ADDRESS.ADDR_TYPE) AND (ADDRESS.RECORD_TYPE = LOCAL_ADDRESS.RECORD_TYPE) AND (ADDRESS.[CEV_NO] = LOCAL_ADDRESS.[CEV_NO])
WHERE (((LOCAL_ADDRESS.CEV_NO) Is Null));

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2013

Commented:
Make a backup and try this:

DELETE * 
FROM ADDRESS 
WHERE ID NOT IN (SELECT ID FROM LOCAL_ADDRESS)

Open in new window

Author

Commented:
I think your idea will work but in my table all following fields are indexed. Meaning that the ID field could be the same value for 2 records but the same 2 records would not have the same value for CEV_NO. Can you give me an idea who I could make it work with all my indexed fields?

Indexed fields:
RECORD_TYPE
CEV_NO
ADDR_TYPE
ID

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2013
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
That works perfect! Now I want the same query to work in Navicat but it does not.

I know that I did not mention this in the original question so I will post another question and award points to you.

Thank you very much for your help and perhaps you can help me out with my other questions too.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.