Merge Join Behaving differently when executed on Server and Local Machine. Outputs completely different

156 views Asked by At

I have an SSIS package which has two source inputs sorted identically and with the same collation. The Merge Join is doing a full outer join.

SSIS Package

Merge Join

The two Queries in the source are as follows:

SELECT [PolicyReference]
      ,[PolicyNarrativeReference]
      ,[PolicyNarrativeTypeCode]
      ,[PolicySystemCode]
      ,[NaturalKeyHash]
      ,[FullRowHash]
      ,[SystemName]
      ,[FunctionalEntityName]
  FROM [KeyHash].[dbo].[vw_Debug_PolicyNarrative_Server_KeyHash]
  ORDER BY [PolicyReference] COLLATE Latin1_General_CI_AS
      ,[PolicyNarrativeReference] COLLATE Latin1_General_CI_AS
      ,[PolicyNarrativeTypeCode] COLLATE Latin1_General_CI_AS
      ,[PolicySystemCode] COLLATE Latin1_General_CI_AS


SELECT [PolicyReference]
      ,[PolicyNarrativeDate]
      ,[PolicyNarrativeReference]
      ,[PolicyNarrativeText]
      ,[PolicyNarrativeTypeCode]
      ,[PolicyNarrativeSystemCode]
      ,[PolicySystemCode]
  FROM [KeyHash].[dbo].[vw_Debug_PolicyNarrative_Server_Source]
  ORDER BY [PolicyReference] COLLATE Latin1_General_CI_AS
      ,[PolicyNarrativeReference] COLLATE Latin1_General_CI_AS
      ,[PolicyNarrativeTypeCode] COLLATE Latin1_General_CI_AS
      ,[PolicySystemCode] COLLATE Latin1_General_CI_AS

Now when this Package is run on the SQL Server through the SQL Agent, The sorting of the two sets becomes skewed and creates 2995 rows which have a blank output

SELECT *
  FROM [KeyHash].[dbo].[Debug_PolicyNarrative_Server_MJ]
  where NaturalKeyHash = 0

Skewed sorting

However when we run the exact same SSIS package locally with the exact same variables, Servers, etc, the sorting works as intended and produces exactly what we want.

SELECT *
  FROM [KeyHash].[dbo].[Debug_PolicyNarrative_Local_MJ]
  where NaturalKeyHash = 0

No problem

The data is exactly the same, the queries are exactly the same, the collations are identical. What on earth could be causing this strange behaviour?

We have tried executing this same package with the same variables and connections but we are seeing the results as above and we are completely stumped.

Also worth mentioning as per David Browne's comment that we do not have Duplicate values within the Order By columns

SELECT COUNT(*)
,[PolicyReference] 
      ,[PolicyNarrativeReference] 
      ,[PolicyNarrativeTypeCode] 
      ,[PolicySystemCode] 
  FROM [KeyHash].[dbo].[vw_Debug_PolicyNarrative_Server_KeyHash]
  GROUP BY [PolicyReference]
      ,[PolicyNarrativeReference]
      ,[PolicyNarrativeTypeCode]
      ,[PolicySystemCode] 
HAVING COUNT(*) > 1

SELECT COUNT(*)
,[PolicyReference] 
      ,[PolicyNarrativeReference] 
      ,[PolicyNarrativeTypeCode] 
      ,[PolicySystemCode] 
  FROM [KeyHash].[dbo].[vw_Debug_PolicyNarrative_Server_Source]
  GROUP BY [PolicyReference] 
      ,[PolicyNarrativeReference] 
      ,[PolicyNarrativeTypeCode] 
      ,[PolicySystemCode] 
HAVING COUNT(*) > 1

Output NoDuplicates

0

There are 0 answers