Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7646699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:16:29+00:00 2026-05-31T10:16:29+00:00

I’ve been using the MERGE operation in SQL Server 2008, but have now come

  • 0

I’ve been using the MERGE operation in SQL Server 2008, but have now come across a similar scenario where I need the same "detection" behaviour (WHEN MATCHED, WHEN NOT MATCHED BY SOURCE, WHEN NOT MATCHED BY TARGET), but want the resulting operations to be on different tables, not the source or the target.

In other words, I have two result-sets, beforeTable and afterTable and I want to do something along the lines of:

MERGE afterTable AS dest
USING beforeTable AS source 
ON (dest.ID = source.ID)
WHEN MATCHED THEN 
    --INSERT tblModifiedItems (ID) VALUES (ID)
WHEN NOT MATCHED BY TARGET THEN
    --INSERT tblRemovedItems (ID) VALUES (ID)
WHEN NOT MATCHED BY SOURCE THEN
    --INSERT tblAddedItems (ID) VALUES (ID)

Presently I do this with three seperate (albeit very simple) queries across the two tables, but the MERGE approach would allow it to be done as a single operation. I just can’t see whether it can be used to target different tables than the two under comparison.

Is this possible, and if so how?

[EDIT]

The reason I want to do this is not due to issues around atomicity of the INSERTS into added/removed/modifed (the whole thing is already in a transaction). It is because I want to be able to generate the three sets of data in a single query, or at least a single statement which also makes the intent very clear.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:16:31+00:00Added an answer on May 31, 2026 at 10:16 am

    short answer would be: no, you can’t. MERGE is an operation between source table and destination table and that’s it. But, I suggest an work-around (there is always one). You’ll have to write some more code, but it will do the trick.

    Do this:

    DECLARE @AuxTable table(    
        id int,
        OLD_name varchar(50),
        new_name varchar(50)
        )
    
    merge table2 as dest
    using table1 as src
    on (dest.id=src.id)
    when not matched by target then
      insert (id, name) values (src.id, src.name)
    when matched then
      UPDATE SET dest.NAME=src.name
    when not matched by SOURCE then
       DELETE
    
      output inserted.id, DELETED.name, INSERTED.NAME
      into @AuxTable
      ;
    
      select * from @AuxTable
    

    see the "output into @AuxTable"? That will insert all the modified values by your merge on a temp table. What yo’ll have to do is run through this table and build a string to execute the SQL on your other table.

    Use this logic:

    • WHEN OLD_NAME = NULL -> INSERT
    • WHEN OLD_NAME != NEW_NAME -> UPDATE
    • WHEN NEW_NAME = NULL -> DELETE
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.