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.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • thanks

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

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