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 7834839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:20:57+00:00 2026-06-02T13:20:57+00:00

The following: MERGE dbo.commissions_history AS target USING (SELECT @amount, @requestID) AS source (amount, request)

  • 0

The following:

MERGE dbo.commissions_history AS target
USING (SELECT @amount, @requestID) AS source (amount, request)
ON (target.request = source.request)
WHEN MATCHED THEN
    UPDATE SET amount = source.amount
WHEN NOT MATCHED THEN
    INSERT (request, amount)
    VALUES (source.request, source.amount);

from https://stackoverflow.com/a/2967983/857994 is a pretty nifty way to do insert/update (and delete with some added work). I’m finding it hard to follow though even after some googling.

Can someone please:

  • explain this a little in simple terms – the MSDN documentation mutilated my brain in this case.
  • show me how it could be modified so the user can type in values for amount & request instead of having them selected from another database location?

Basically, I’d like to use this to insert/update from a C# app with information taken from XML files I’m getting. So, I need to understand how I can formulate a query manually to get my parsed data into the database with this mechanism.

  • 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-06-02T13:21:00+00:00Added an answer on June 2, 2026 at 1:21 pm

    If you aren’t familiar with join statements then that is where you need to start. Understanding how joins work is key to the rest. Once you’re familiar with joins then understanding the merge is easiest by thinking of it as a full join with instructions on what to do for rows that do or do not match.

    So, using the code sample provided lets look at the table commissions_history

    |  Amount  |   Request  |   <other fields> |
    --------------------------------------------
    |  12.00   |   1234     |   <other data>   |
    |  14.00   |   1235     |   <other data>   |
    |  15.00   |   1236     |   <other data>   |
    

    The merge statement creates a full join between a table, called the “target” and an expression that returns a table (or a result set that is logically very similar to a table like a CTE) called the “source”.

    In the example given it is using variables as the source which we’ll assume have been set by the user or passed as a parameter.

    DECLARE @Amount Decimal = 18.00;
    DECLARE @Request Int = 1234;
    
    MERGE dbo.commissions_history AS target       
    USING (SELECT @amount, @requestID) AS source (amount, request)       
    ON (target.request = source.request)   
    

    Creates the following result set when thought of as a join.

    |  Amount  |   Request  |   <other fields> | Source.Amount | Source.Request  |
    ------------------------------------------------------------------------------
    |  12.00   |   1234     |   <other data>   |   18.00       |     1234        |
    |  14.00   |   1235     |   <other data>   |   null        |     null        |
    |  15.00   |   1236     |   <other data>   |   null        |     null        |
    

    Using the instructions given on what to do to the target on the condition that a match was found.

    WHEN MATCHED THEN        
    UPDATE SET amount = source.amount    
    

    The resulting target table now looks like this. The row with request 1234 is updated to be 18.

    |  Amount  |   Request  |   <other fields> |
    --------------------------------------------
    |  18.00   |   1234     |   <other data>   |
    |  14.00   |   1235     |   <other data>   |
    |  15.00   |   1236     |   <other data>   |
    

    Since a match WAS found nothing else happens. But lets say that the values from the source were like this.

    DECLARE @Amount Decimal = 18.00;
    DECLARE @Request Int = 1239;
    

    The resulting join would look like this:

    |  Amount  |   Request  |   <other fields> | Source.Amount | Source.Request  |
    ------------------------------------------------------------------------------
    |  12.00   |   1234     |   <other data>   |   null        |     null        |
    |  14.00   |   1235     |   <other data>   |   null        |     null        |
    |  15.00   |   1236     |   <other data>   |   null        |     null        |
    |  null    |   null     |   null           |   18.00       |     1239        |
    

    Since a matching row was not found in the target the statement executes the other clause.

    WHEN NOT MATCHED THEN                                 
    INSERT (request, amount)                                 
    VALUES (source.request, source.amount);  
    

    Resulting in a target table that now looks like this:

    |  Amount  |   Request  |   <other fields> |
    --------------------------------------------
    |  12.00   |   1234     |   <other data>   |
    |  14.00   |   1235     |   <other data>   |
    |  15.00   |   1236     |   <other data>   |
    |  18.00   |   1239     |   <other data>   |
    

    The merge statements true potential is when the source and target are both large tables. As it can do a large amount of updates and/or inserts for each row with a single simple statement.

    A final note. It’s important to keep in mind that not matched defaults to the full clause not matched by target, however you can specify not matched by source in place of, or in addition to, the default clause. The merge statement supports both types of mismatch (records in source not in target, or records in target not in source as defined by the on clause). You can find full documentation, restrictions, and complete syntax on MSDN.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Merge statement I'm working with: MERGE dbo.UnitOfMeasure AS target USING
The following statement is in a Stored Procedure MERGE INTO table1 a USING (SELECT
create procedure [dbo].[teststoredproc] (@startDate datetime, @endDate datetime) as begin set nocount on; merge TargetTable
I'd like to merge the results of the following three select statements horizontally. I
I'm using the following post build actions in a project, to merge a lib
how would i merge the following two sql select statements? //select all rows from
Is it possible to merge elements using XSLT. If I have the following XML
I am using following code to merge 2 different bitmap into 1. public Bitmap
Update: Oh good grief, it was all a red herring following a bad merge.
I want to merge data. Following are my MySQL tables. I want to use

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.