Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MERGE question

Status
Not open for further replies.

djj55

Programmer
Feb 6, 2006
1,761
US
Hello, SQL 2008R2
I was looking at the MERGE command with respect to a process I am currently using. I thought the INSERT/UPDATE done by MERGE would speed things up.

Problem: Currently during the update a field is updated in target that indicates date of change for one column. Can this be done with a merge?

Code:
UPDATE target
SET ....
-- various columns updated then
  , myColumn = #Changes.myColumn
  , myColumnChanged = 
    CASE 
-- old value not equal to new value or no old value but have a new value
      WHEN target.myColumn <> #Changes.myColumn OR 
        (target.myColumn IS NULL AND #Changes.myColumn IS NOT NULL) 
      THEN CAST(GETDATE() AS DATE) 
    ELSE myColumnChanged 
    END
FROM target 
INNER JOIN #Changes  
    ON target.PKvalue = #Changes.PKvalue

Note that the current process works so I do not need to change it but would like to know if it is possible to do the update within the MERGE or do I need to use OUTPUT and do a second update.
Thank you,

djj
The Lord is my shepherd (Psalm 23) - I need someone to lead me!
 
Thank you markros. I entered MATCHED for BOL search and found "Inserting, Updating, and Deleting Data by Using MERGE" which covers MATCHED, which may work.

As stated this is just a learning exercise which you helped along.

Thanks again,

djj
The Lord is my shepherd (Psalm 23) - I need someone to lead me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top