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

  • Home
  • SEARCH
  • 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 1089347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:12:28+00:00 2026-05-16T23:12:28+00:00

I have the following tables: Entry EntryID – int EntryDate – datetime Hour EntryID

  • 0

I have the following tables:

Entry
EntryID – int
EntryDate – datetime

Hour
EntryID – int
InHour – datetime
OutHour – datetime

For each registry in the Entry table, there should be at least one (could be many) registries on the Hour table, like so:

Entry
EntryID: 8
EntryDate: 9/9/2010 12:31:25

Hour
EntryID: 8
InHour: 9/9/2010 12:31:25
OutHour: 9/9/2010 18:21:19

Now, this information is stored on 2 equal databases, one on local machine and one on a server. I’m trying to write a query that will delete all the information that has already been passed to the server under the condition that the registries that do not have an OutHour (null) will not be deleted.

I wrote the following query:

DELETE from [dbo].[Entry]
 WHERE [dbo].[Entry].[EntryID] IN (SELECT [EntryID] 
                                     FROM [LINKEDSERVER].[MYDATABASE].[dbo].[Entry]) 
  AND [dbo].[Entry].[EntryID] IN (SELECT [EntryID] 
                                    FROM [dbo].[Hour] 
                                   WHERE [OutHour] IS NOT NULL)

DELETE from [dbo].[Hour]
 WHERE [dbo].[Hour].[InHour] IN (SELECT [InHour] 
                                   FROM [LINKEDSERVER].[MYDATABASE].[dbo].[Hour]) 
   AND [dbo].[Hour].[OutHour] IS NOT NULL 

AFAIK, this query first checks in the Entry table and will delete any registries that are already on the server and do not have a corresponding Hour registry that has a null OutHour. However today I found out that an Entry record was deleted but the corresponding Hour wasn’t (it had a null OutHour).

What am I doing wrong? Any help is appreciated.

Thanks!

  • 1 1 Answer
  • 1 View
  • 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-16T23:12:29+00:00Added an answer on May 16, 2026 at 11:12 pm

    My very first suggestion is to put a foreign key relationship between the two on EntryID. This will prevent any deletions from the Entry table without first removing all instances from the Hour table.

    Secondly, with a foreign key in place you have to do it from the child to the parent (aka, start at the bottom of the hierarchy). This means i would do this first:

    delete from dbo.Hour where OutHour is not null
    delete e
    from dbo.Entry e
    left outer join dbo.Hour h
    on e.entryid=h.entryid
    where h.entryid is null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.