• terry999 (10/2/2012)


    Thank you. Your example is easier to read.

    BTW I was restricting to only chnage rows by comparing the checksum values.

    I didn't know you could add additional filters after is matched i.e. target.name != source.name

    Sorry theres a coding error I believe it should read

    MERGE tblCustomer AS target

    USING (SELECT * FROM tblUpdates ) AS source

    ON (target.cid = source.cid)

    WHEN MATCHED

    AND (target.name!=source.name

    or target.country!=source.country)

    THEN UPDATE SET name = source.name, country = source.country

    WHEN NOT matched

    THEN INSERT

    VALUES (CID,Name, Country)

    OUTPUT $action

    , getdate()

    , CASE WHEN $action = 'INSERT' THEN inserted.name ELSE deleted.name END

    , CASE WHEN $action = 'INSERT' THEN inserted.country ELSE deleted.country END

    , binary_checksum(inserted.name, inserted.country)

    ,binary_checksum(deleted.name, deleted.country)

    INTO #tAudit

    _________________________________________________________________________
    SSC Guide to Posting and Best Practices