Home Forums SQL Server 2008 T-SQL (SS2K8) comparison in the merge statement about null values RE: comparison in the merge statement about null values

  • mburbea (5/7/2014)


    I absolutely despise how verbose the way the is variable changed check is

    The only idea that leaps to mind is that while the OUTPUT clause does not allow a subquery, it does allow a scalar function:

    CREATE FUNCTION dbo.IsDistinctFrom

    (

    @value1 sql_variant,

    @value2 sql_variant

    )

    RETURNS bit

    WITH SCHEMABINDING

    AS

    BEGIN

    RETURN

    CASE WHEN NOT EXISTS

    (

    SELECT @value1

    INTERSECT

    SELECT @value2

    )

    THEN 1

    ELSE 0

    END;

    END;

    The OUTPUT clause would then become something like:

    OUTPUT

    Inserted.$identity AS id,

    dbo.IsDistinctFrom(Inserted.col1, Deleted.col1) AS col1,

    dbo.IsDistinctFrom(Inserted.col2, Deleted.col2) AS col2,

    dbo.IsDistinctFrom(Inserted.isActive, Deleted.isActive) AS isActive

    It's not perfect, but perhaps it gives you some ideas.