Creating a merge that contains an IF condition

  • Hi,

    want to merge two tables on two columns, partid and serialid.

    but then I want to check if the purchasedate also match, if it matches I leave it alone, otherwise I update the date and source. i having having issues with the if part.

    MERGE [dbo].[tableone] T
    USING [dbo].tabletwo S
    ON (S.PARTID = T.PART_ID AND S.SERIAL = T.SERIAL)
    WHEN MATCHED
    --i would like to check here if the dates also match
    -- do nothing
    -- if they dont then update
    THEN UPDATE SET
    T.DATE = S.DATE...
    ---and then otherwise insert.
  • Have you tried something like this? The HOLDLOCK helps avoid potential concurrency issues.

    MERGE [dbo].[tableone] WITH (HOLDLOCK) T
    USING [dbo].tabletwo S
    ON (S.PARTID = T.PART_ID AND S.SERIAL = T.SERIAL)
    WHEN MATCHED AND (S.DATE <> T.DATE or T.DATE IS NULL)
    THEN UPDATE SET
    T.DATE = S.DATE...
    ---and then otherwise insert.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • thanks

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply