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!