Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 499177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:57:32+00:00 2026-05-13T05:57:32+00:00

I have two tables called TableA and TableB. TableA has the following fields: TableA_ID

  • 0

I have two tables called TableA and TableB.

TableA has the following fields:

TableA_ID
FileName

TableB has the following fields:

TableB_ID
TableA_ID
CreationDate

There is a foreign key link between the two tables on the TableA_ID field

I need to delete records from both tables. I need to look at the “CreationDate” on TableB and if it’s after a certain date, delete that record. I will also need to delete the record in TableA with the same TableA_ID as the record in TableB

There may be several records in TableB that use the TableA_ID (a one to many relationship). So I can’t delete the record in TableA if entries in TableB still use it.

I know this can’t be done in a single statement but am happy to do it in a transaction. The problem I have is I’m not sure how to do this. I’m using MS SQL server 2008. I don’t want to use triggers if possible.

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:57:32+00:00Added an answer on May 13, 2026 at 5:57 am

    Can there be records in TableA with no matching record in TableB? If not, then we know after we delete from TableB, we can delete any non-matching records in TableA:

    begin transaction
    delete from TableB
    where CreationDate > @SomeDate
    
    delete from TableA
    where TableA_ID not in (select TableA_ID from TableB)
    end transaction
    

    Otherwise:

    begin transaction
    -- Save the TableA_IDs being deleted:
    select distinct TableA_ID
    into #TableA_Delete
    from Table_B
    where CreationDate > @Somedate
    
    -- Depending on the expected size of #TableA_Delete, you may want 
    -- to create an index here, to speed up the delete from TableA.
    
    delete from TableB
    where CreationDate > @SomeDate
    
    delete from TableA
    where TableA_id in (select TableA_Id from #TableA_Delete)
    and TableA_id not in (select TableA_id from TableB)
    commit transaction
    

    NOTE Both above solutions need error handing added.

    Also, see NYSystemsAnalyst for another method of storing the IDs temporarily.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables. One of them called conversations has the following data. ConversationID
This is an odd one... I have two tables tableA and tableB tableB has
I have two tables: servers and stats servers has a column called id that
I have two tables Department Professor in which Department has an attribute called HeadID
Say I have two tables called A (fields: id, phase, name) and B(fields: id,
I have a table called user_relationship. which has two foreign keys refering back to
I have two tables in my database. The first one, [nodeActivity] has the following
I have two tables called categories and topics. categories has columns category_id and category_title.
I have two tables: one called Projects, and another called Documents (projects has many
I basically have two tables called images and users . The images table has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.