Question

Which statement is perfect or better when dealing with billion of records for comparing NULL's in merge statement. I have tried with SET ANSI_NULLS OFF but that didn't work in merge statement. Here is my two ways

ISNULL(SRCColumn,-11111) = ISNULL(DSTColumn, -11111)

Or

SRCColumn = DSTColumn OR (SRCColumn  IS NULL AND DSTColumn  IS NULL)

Please let me know if there is any better way to deal with it. As I have around 15 columns to compare.

Was it helpful?

Solution

SRCColumn = DSTColumn OR (SRCColumn IS NULL AND DSTColumn IS NULL)

I'd suggest that you use this version because it most accurately expresses what you want SQL Server to do.

Both statements are logically equivalent (unless -11111 is a legal value for the column), however this statement is much more recognizable, and there's probably only a negligible difference in the run-time performance of the two statements.

OTHER TIPS

If you are more concerned with succinctness than performance, CHECKSUM() is also an option. It will match NULL -> NULL:

MERGE A
USING B
ON A.Key = B.Key
WHEN MATCHED AND CHECKSUM(A.Col1, A.Col2, ... ) <> CHECKSUM(B.Col1, B.Col2, ... )
THEN UPDATE SET Col1 = B.Col1, Col1 = B.Col2, ...

How about using the NOT comparison on matching:

MERGE [TGT]
USING [SRC]
ON [SRC].Key = [TGT]. Key
…

WHEN MATCHED AND
(
NOT ([TGT].[dw_patient_key] = [SRC].[dw_patient_key] OR ([TGT].[dw_patient_key] IS NULL AND [SRC].[dw_patient_key] IS NULL))
OR NOT ([TGT].[dw_patient_key] = [SRC].[dw_patient_key] OR ([TGT].[dw_patient_key] IS NULL AND [SRC].[dw_patient_key] IS NULL))
...
)
THEN UPDATE
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top