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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:26:57+00:00 2026-06-07T00:26:57+00:00

Jacob asked the perfect question: give me the MERGE syntax . Every answer out

  • 0

Jacob asked the perfect question: give me the MERGE syntax.

Every answer out there immediately jumps to the most complicated case they can think of; obscuring the syntax with extraneous confusion.

Marc gave an answer:

MERGE 
   member_topic AS target
USING 
   someOtherTable AS source
ON 
   target.mt_member = source.mt_member 
   AND source.mt_member = 0 
   AND source.mt_topic = 110
WHEN MATCHED THEN 
   UPDATE SET mt_notes = 'test'
WHEN NOT MATCHED THEN 
   INSERT (mt_member, mt_topic, mt_notes) VALUES (0, 110, 'test')
; 

Looking at this answer, i am as confused as Jacob was:

I don’t have a someOtherTable

Marc suggested that someOtherTable is a dummy placeholder value – it doesn’t matter that you don’t have that table.

i try it, and SQL Server does complain

Invalid object name ‘someOtherTable’.

That leaves me struggling to understand what the USING in USING foo is for if it’s not important (except actually important).

What is USING using when it’s using foo when i use SQL Server 2008 MERGE syntax?


Bonus Question

What is the UPSERT syntax using MERGE:

IF (rowExists)
   UPDATE Users SET Firstname='Ian', LastName='Boyd' WHERE Username='iboyd'
ELSE
   INSERT INTO Users (UserGUID, Username, FirstName, LastName, AuthenticationMethod)
   VALUES ('{77410DC5-7A3E-4F1A-82C6-8EFB3068DE66}', 'iboyd', 'Ian', 'Boyd', 'Windows')

becomes (exact code i tried):

begin transaction

    MERGE 
       Users
    USING 
       foo
    ON  
       Users.UserName = foo.UserName
    WHEN MATCHED THEN
        UPDATE SET Firstname = foo.FirstName, Lastname = foo.LastName
    WHEN NOT MATCHED THEN
        INSERT (UserGUID, Username, FirstName, LastName, AuthenticationMethod)
        VALUES ('{77410DC5-7A3E-4F1A-82C6-8EFB3068DE66}', 'iboyd', 'Ian', 'Boyd', 'Windows')
    ; --A MERGE statement must be terminated by a semi-colon (;).

rollback

Msg 208, Level 16, State 1, Line 3
Invalid object name 'foo'.

?

With a Users table that contains the columns:

UserGUID uniqueidentifier
Username varchar(50)
FirstName varchar(50)
LastName varchar(50)
AuthenticationMethod varchar(50)

Update:

USING <table_source> 

Where table_source is:

table_or_view_name [ [ AS ] table_alias ] [ <tablesample_clause> ] 
    [ WITH ( table_hint [ [ , ]...n ] ) ] 
| rowset_function [ [ AS ] table_alias ] 
    [ ( bulk_column_alias [ ,...n ] ) ] 
| user_defined_function [ [ AS ] table_alias ]
| OPENXML <openxml_clause> 
| derived_table [ AS ] table_alias [ ( column_alias [ ,...n ] ) ] 
| <joined_table> 
| <pivoted_table> 
| <unpivoted_table> 

Where joined_table is:

undefined

Where pivoted_table is:

undefined

Where unpivoted_table is:

undefined

  • 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-07T00:26:58+00:00Added an answer on June 7, 2026 at 12:26 am

    A merge has a table source and a target table. This introduces the source table (which need not be an actual physical table, just a result set).

    The grammar is indicated in your question. To merge from another table or view use

    MERGE 
       Users
    USING SomeOtherTableName AS foo /*Alias is optional*/
    ON /* ... */
    

    Or you can use <unpivoted_table> for example

    MERGE 
       Users
    USING master..spt_values
    UNPIVOT (X FOR Y IN ([high],[low])) AS foo 
    ON  
       Users.Username = foo.Y 
    WHEN MATCHED THEN
        UPDATE SET FirstName = foo.Y
    WHEN NOT MATCHED THEN
        INSERT (UserGUID, Username, FirstName, LastName, AuthenticationMethod)
        VALUES (foo.Y, foo.Y, foo.Y, foo.Y, foo.Y);
    

    For your bonus question you can use the VALUES clause here as part of the derived_table option.

    MERGE Users
    USING (VALUES ('{77410DC5-7A3E-4F1A-82C6-8EFB3068DE66}',
          'iboyd',
          'Ian',
          'Boyd',
          'Windows')) AS foo(UserGUID, Username, FirstName, LastName, AuthenticationMethod)
    ON Users.UserName = foo.UserName
    WHEN MATCHED THEN
      UPDATE SET Firstname = foo.FirstName,
                 Lastname = foo.LastName
    WHEN NOT MATCHED THEN
      INSERT (UserGUID,
              Username,
              FirstName,
              LastName,
              AuthenticationMethod)
      VALUES (UserGUID,
              Username,
              FirstName,
              LastName,
              AuthenticationMethod); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering if anyone out there knows of a standard survey (preferably based off
I've been looking for the answer to this question for a while now but
I know there are DSSS , BUD /build, Orbit by Jacob Carlborg and official
In regard to my question here , Jacob Relkin suggested a great solution of
I want to replace names in a text with a link to there profile.
i want to learn jacob for doing tasks with excel. looking for any simple
I am using JACOB to access system information through WMI. I have not found
Lets say I have the following table User_ID Manager_ID --------------------- Linda Jacob Mark Linda
I have used the Java Jacob library to communicate with inedesign server application which
Is there any way in c# .NET 2.0! to combine multiple Predicates? Let's say

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.