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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:24:53+00:00 2026-06-16T01:24:53+00:00

Earlier today, I asked this question and got the answer I was looking for.

  • 0

Earlier today, I asked this question and got the answer I was looking for. Now I have a follow-up question:

What I want:

I want the MERGE to compare each column value, per row, in the target table against the corresponding value in the source table, and make any updates based on the logic separated by OR in the WHEN MATCHED AND block.

I am afraid that the code I’ve written (pictured below) will make the updates listed in the THEN UPDATE SET block if any of the logic separated by OR in the WHEN MATCHED AND block is true.

If my hunch is correct, do you have any suggestions for how to re-write the code to have it behave like I want it to behave?

enter image description here

  • 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-16T01:24:55+00:00Added an answer on June 16, 2026 at 1:24 am

    Not having your data, and not wanting to re-type your query from an image, I created a sample that I think demonstrates what you want:

    create table t (ID int not null,Col1 int null,Col2 int null)
    create table s (ID int not null,Col1 int null,Col2 int null)
    
    insert into t(ID,Col1,Col2) values (1,1,null),(2,null,2)
    insert into s(ID,Col1,Col2) values (1,3,4),(2,5,6),(3,7,8)
    
    ;merge into t
    using s
    on t.ID = s.ID
    when not matched then insert (ID,Col1,Col2) values (s.ID,s.Col1,s.Col2)
    when matched then update
    set Col1 = COALESCE(t.Col1,s.Col1),
    Col2 = COALESCE(t.Col2,s.Col2)
    ;
    select * from t
    

    Result:

    ID          Col1        Col2
    ----------- ----------- -----------
    1           1           4
    2           5           2
    3           7           8
    

    Where the key is to use COALESCE to avoid updating a column value if it already has one (which I think is what you’re trying to achieve)

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

Sidebar

Related Questions

I asked this goofy question earlier today and got good answers. I think what
This is a follow-up to another question I asked earlier today. I am creating
I have asked this question earlier today but I didn't provide enough information and
I know I have asked this question in a different manner earlier today but
This is a follow up to a question I asked earlier today regarding the
Earlier today I asked a question and got a good answer here . However
I asked a question here earlier today and got that fixed, but now I
I asked another version of this question on the gamedev.SE site earlier today but
earlier today I asked this question . So since moq creates it's own class
I asked this question earlier today. After trying out suggestions from a few members,

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.