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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:46:09+00:00 2026-05-20T01:46:09+00:00

I have looked at various example of the SQL Merge statement.. all of which

  • 0

I have looked at various example of the SQL Merge statement.. all of which look excellent, albeit for some reason I cannot seem to get the correct/expected results from my Merge test.

Quick overview:
I have a simple table with some design data in it.. and reading about MERGE seems to point to a more efficient way of doing an ‘upsert’ (i.e: an Insert or Update depending if the record exists or not).

So the SQL 2008 code goes something like this (sorry if it is not fully complete, as I am working on it!):

This will be in a stored procedure, so the @values are obviously the passed params..

merge designs as ds
using ( select designname, designcode from designs) as dsi
on (@passedDesignName = dsi.designname and @passedDesignCode = dsi.designcode)
when matched then
    update set ds.designname = @passedDesignName, ds.designcode = @passedDesignCode
when not matched then
    insert (designname, designcode)
    values (@passedDesignName, @passedDesignCode)

The issue seems to be out of the 7 records I am testing with, ALL of them seem to be updated, when clearly I can only see one record that matches the update.. and the strange thing is if I pass some NEW data (designname and designcode), I seem to get a repeating insert.. from my last test it seemed 7 new inserts which I am guessing isn’t just a fluke..

Hope I have explained this correctly.. part of attacking something new is mostly getting the context correct right?

Thanks in advance for any feedback.

P.S: Sorry, there is a semi-colon at the end of the merge statement! to complete the parse checking/syntax.

  • 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-20T01:46:10+00:00Added an answer on May 20, 2026 at 1:46 am

    You’re using designs as both the target and the source table:

    merge designs as ds
    using ( select designname, designcode from designs) as dsi
    

    This is equivalent to:

    merge designs as ds
    using designs as dsi
    

    Instead, try to pass the variables as the source table:

    merge designs as ds
    using (
          select  @passedDesignName as designname
          ,       @passedDesignCode as designcode
          ) as dsi
    on (ds.esignName = dsi.designname and ds.designCode = dsi.designcode)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have looked all over and cannot figure out why this code isn't working.
I have looked at various methods ranging from string.erase/ ispunct etc. and I cant
I have looked on the web to find a list of all Python keywords
I am pulling my hair out with this one. I have looked and cannot
I have written various C# console based applications, some of them long running some
I have some data in R with various variables for my cases: B T
I have looked all over for pthread_join examples, I am having troubles debugging this
I'm trying to merge\combine two datatables. I've looked at various examples and answers but
I have looked at some of the DICOM standard and the Wikipedia page (and
In pretty much all applications that have a menu bar, some of the items

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.