Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

TSQL Merge - Multiple statements after THEN

$
0
0

I'm using a MERGE statement to merge two tables.

When I have a match in keys, but the data is different I want to inactivate the old/existing record with an update statement where I fill the EndDate column with GetDate() and the Active active with false.... (No problem)

But I also want to insert a new record with the same key, new data, fill startdate with GetDate() and Active with true.

Is that possible?

--something like....

WHEN MATCHED AND NOT (
    MySource.Hash = MyTarget.Hash
)
THEN
    -- inactivate old record
    UPDATE
    SET MyTarget.Active = 0
    ,   MyTarget.DateEnd = GetDate()

    -- insert new version of record
    INSERT (column1, column2, DateStart, Active)
    VALUES (MySource.column1, MySource.column2, GetDate(), 1)

WHEN NOT MATCHED BY TARGET
THEN
    -- etc. etc.

 

Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>