I am using a MERGE with OUTPUT wrapped inside an INSERT to do a typical TypeII table insert update.
In the merge statement, where I need to check if any of the attributes have changed, I will gave something like
... WHEN MATCHED AND (-- condition to check if any attributes have changed)
THEN UPDATE blah blah -- expire the row
The question I have is regarding the "(-- condition to check if any attributes have changed)" in the above pseudo code.
If there is a match in the target, I want to check if any of the dimension attributes have changed. This will typically look something like
... WHEN MATHED AND (
src.Attr1 != tgt.Attr1 OR
src.Attr2 != tgt.Attr2 OR
src.AttrN != tgt.AttrN
)
But this check isn't sufficient because of the possibility that some of the attribute columns can be NULL. So to account for that, I have to do something like
... WHEN MATHED AND (
src.Attr1 != tgt.Attr1 OR (src.Attr1 IS NULL AND tgt.Attr1 IS NOT NULL) OR (src.Attr1 IS NOT NULL AND tgt.Attr1 IS NULL) OR
src.Attr2 != tgt.Attr2 OR (src.Attr2 IS NULL AND tgt.Attr2 IS NOT NULL) OR (src.Attr2 IS NOT NULL AND tgt.Attr2 IS NULL) OR
src.AttrN != tgt.AttrN OR (src.AttrN IS NULL AND tgt.AttrN IS NOT NULL) OR (src.AttrN IS NOT NULL AND tgt.AttrN IS NULL)
)
I hope you can see what I am trying to check.... Is this the only way to do this? Or is there something more condensed?