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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:07:50+00:00 2026-05-28T14:07:50+00:00

In SQL Server with a MERGE code, everything is fine except when there are

  • 0

In SQL Server with a MERGE code, everything is fine except when there are 2 nullable columns.

If I pass a null value and the target isn’t null, MERGE doesn’t see a difference (evals against null = false). If I use IsNull on both sides (source & target) that works, but has the issue of potentially mis-evaluating a value.

What I mean by the last statement is, if I say:

WHEN MATCHED AND NOT (IsNull(tgt.C, 0) = IsNull(src.C, 0)) THEN

then if tgt.C is null and src.C = 0, no update will be performed. No matter what substitute value I choose, I’ll have this problem.

I also tried the "AND NOT (…true…)" syntax since BOL states that evaluations against null result in FALSE. However, it seems they actually result in NULL and do not result in my multi-part statement becoming false.

I thought one solution is to use NaN or -INF or +INF since these are not valid in target. But I can’t find a way to express this in the SQL.

Any ideas how to solve this?

EDIT:

The following logic solves the problem, but it’s verbose and won’t make for fast evals:

declare @i int, @j int

set @j = 0
set @i = 0

if ISNULL(@i, 0) != ISNULL(@j, 0) OR 
    ((@i is null or @j is null) and not (@i is null and @j is null))
    print 'update';
  • 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-28T14:07:50+00:00Added an answer on May 28, 2026 at 2:07 pm

    In SQL Server 2022 you can use

    WHEN MATCHED AND tgt.C IS DISTINCT FROM src.C
    

    In previous versions you can use

    WHEN MATCHED AND EXISTS (SELECT tgt.C EXCEPT SELECT src.C)
    

    The second version can still be more compact if you need to do this check across multiple columns.

    WHEN MATCHED AND EXISTS (SELECT tgt.A, tgt.B, tgt.C 
                             EXCEPT 
                             SELECT src.A, src.B, src.C)
    

    See this article for more on this issue.

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

Sidebar

Related Questions

We're replicating a database between London and Hong Kong using SQL Server 2005 Merge
We're having a problem with a merge replication. Our publisher runs SQL Server 2008,
I'm adapting a large number of SQL Server 2005 scripts to merge two of
Here is the setup: SQL Server 2008 Enterprise Server with a Merge Publication. SQL
I am learning and using SQL Server 2008 new Merge statement, merge statement will
I am executing the following merge statement in SQL Server 2008: MERGE PopulationData AS
I have a database running under Sql server 2005 with merge replication. I want
What's the difference between peer-to-peer replication and merge replication using SQL Server?
I have 2 servers running SQL Server 2005 with merge replication set up between
I am attempting to use the SQL SERVER 2008 MERGE statement in a stored

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.