DELETE using values from another table

DELETE using values from another table

I am using MS Access. I have two tables: A and B. A has the fields ID, DATE and others. B has the field ID and others. The tables are related by the field ID. I need to eliminate some records of B depending of the value of DATE of A. How can I do this?

    Requires Free Membership to View

    By submitting your registration information to SearchOracle.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchOracle.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

There are two methods. The first uses WHERE EXISTS syntax:

delete from B
 where exists
       ( select 1 from A
          where ID = B.ID
            and [DATE] = #2002-09-28# )

The second method uses WHERE IN syntax:

delete from B
 where ID in
       ( select ID from A
          where [DATE] = #2002-09-28# )

Both methods are equivalent.

By the way, I used square brackets around the column called DATE. I would strongly urge you not to name your columns using a reserved word. That's just asking for trouble, like syntax errors.

For More Information


This was first published in October 2002

Join the conversationComment

Share
Comments

    Results

    Contribute to the conversation

    All fields are required. Comments will appear at the bottom of the article.