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.
September 16, 2020 at 2:57 pm
thanks
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy