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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:27:59+00:00 2026-05-19T02:27:59+00:00

I am using a MERGE statement to basically do an UPSERT. For the rows

  • 0

I am using a MERGE statement to basically do an UPSERT. For the rows that are not present in the destination, I would like them inserted in a certain order. Unfortunately, it seems the ORDER BY clause isn’t supported with a merge statement. Is there any way do this in one statement? See example for better idea of what I am trying to do:

CREATE TABLE #destination (ident int not null identity(1,1), id int not null,   value int not null)
INSERT INTO #destination (id,value) VALUES (1,50)

CREATE TABLE #source (id int not null, value int not null)
INSERT INTO #source (id,value) VALUES (1,100),(3,300),(2,200)

MERGE #destination d
USING #source s
    ON d.id = s.id
WHEN MATCHED THEN 
    UPDATE 
    SET d.value = s.value
WHEN NOT MATCHED THEN
    INSERT (id,value)
    VALUES (s.id,s.value);

SELECT * FROM #destination ORDER BY ident
/* 
WILL LIKELY SEE:
    1, 1, 100
    2, 3, 300
    3, 2, 200
WANT TO ACHIEVE:
    1, 1, 100
    2, 2, 200
    3, 3, 300
*/

The reason I want to do this is I would like to write a unit test for my code that performs this merge and want the insertions in a deterministic order. I know there are ways to get around this, but if there is a way to order the insertion of a MERGE it would be the easiest.

  • 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-19T02:27:59+00:00Added an answer on May 19, 2026 at 2:27 am

    So right now this is not possible. From the documentation on MERGE, the operation of inserts, deletes, and updates is unordered; but it was burried in it’s explanation of how TOP affects it:

    The TOP clause further reduces the
    number of joined rows to the specified
    value and the insert, update, or
    delete actions are applied to the
    remaining joined rows in an unordered
    fashion. That is, there is no order in
    which the rows are distributed among
    the actions defined in the WHEN
    clauses. For example, specifying TOP
    (10) affects 10 rows; of these rows, 7
    may be updated and 3 inserted, or 1
    may be deleted, 5 updated, and 4
    inserted and so on.

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

Sidebar

Related Questions

i'm creating a procedure to update/insert a table using merge statement(upsert).now i have a
I am using SQL Server 2008 Enterprise DB and using the new Merge statement
I am learning and using SQL Server 2008 new Merge statement, merge statement will
I have a merge statement that needs to compare on many columns. The source
I am trying to merge multiple excel files using DataTable.Merge() option For Each fileName
Is it possible to merge elements using XSLT. If I have the following XML
Using the svnmerge.py tool it is possible to merge between branches, up and down.
We're currently using WinCVS but it's slow and has no merge dialog. I'm looking
I need to do a simple mail merge in OpenOffice using C++, VBScript, VB.Net
We're replicating a database between London and Hong Kong using SQL Server 2005 Merge

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.