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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:10:19+00:00 2026-05-18T21:10:19+00:00

Synopsis I’ve got a query with a WHERE clause that contains a condition that

  • 0

Synopsis

I’ve got a query with a WHERE clause that contains a condition that checks a NULLable column against a parameter that may be NULL, like so:

SELECT ...
FROM Table
WHERE NullableColumn = @NullableParameter

From my understanding, SQL Server is supposed to evaluate NULL = NULL non-NULL = NULL as false always, meaning if @NullableParameter is NULL then the above query would return zero records.

That’s the behavior I see when running the query in a query window, but when I use the same query in a stored procedure and when @NullableParameter is NULL it is returning all records where NullableColumn is NULL. In short, it seems like the sproc is saying NULL = NULL is true.

That’s the problem/question is a nutshell. The actual WHERE clause is a bit more involved and is presented with more detail in the section below.

The Gritty Details

I have a database with a table called StudyResults. Each record is uniquely identified by a StudyResultId primary key field. There is also a ParticipantId field, that indicates the subject involved in the study, and GroupId, which identifies what group the subject belonged to, if any. If the study was a single-person study, GroupId is NULL. ParticipantId cannot be NULL.

I have a stored procedure that needs to update some records in the StudyResults table for a particular study, but here’s the rub – if the study is single-person study then I need to just update that one row; if it was a group study I want to update all rows in StudyResults for that group, for that subject.

This isn’t too hard to accomplish. The stored procedure is passed a StudyResultId and then runs the following T-SQL to determine the GroupId and ParticipantId values for that row:

DECLARE @GroupId INT, @ParticipantId INT
SELECT @GroupId = GroupId,
       @ParticipantId = ParticipantId
FROM StudyResults
WHERE StudyResult = @StudyResultId

Next, I create a CURSOR to enumerate the StudyResults records of interest. Note the WHERE clause, which says, "Give me the records where StudyResultId equals the @StudyResultId passed into the sproc or the GroupId and ParticipantId values line up with the GroupId and ParticipantId values for the StudyResults record of interest.

DECLARE resultsToEnumerate CURSOR LOCAL FAST_FORWARD FOR 
SELECT DISTINCT StudyResultId
FROM StudyResults
WHERE StudyResult = @StudyResultId OR (GroupId = @GroupId AND ParticipantId= @ParticipantId)

If @GroupId is NULL, then comparing GroupId = @GroupId should always be false, right? Because for SQL Server, NULL = NULL is false, and non-NULL = NULL is false.

But here’s what it gets weird – if I run the above statements from a query window and use a @StudyResultId for a single-person study the CURSOR contains what I expect – namely, a single record. However, if I put the exact same code in a stored procedure and run it the CURSOR contains all of the single-person studies for that participant! It’s as if it is saying @GroupId is NULL, so I’ll return all records where GroupId is NULL, but why? NULL = NULL should never return a record, right?

In fact, if I go into the sproc and change the WHERE clause and replace GroupID = @GroupID with NULL = NULL I see the same results – all single-person studies for the participant in question. So it clearly is evaluating NULL = NULL to true in the sproc (or ignoring it).

The Workaround

I can "fix" this in the stored procedure by adding an additional check in the parentheses to ensure GroupId IS NOT NULL like so:

WHERE ActivityID = @ActivityID OR (GroupID IS NOT NULL AND GroupID = @GroupID AND PatientID = @PatientID)

This is what I have done, but I am perplexed as to why the WHERE clause is being evaluated differently in my stored procedure.

  • 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-18T21:10:20+00:00Added an answer on May 18, 2026 at 9:10 pm

    Try altering the stored procedure in a session after setting ANSI_NULLS to ON.

    SET ANSI_NULLS ON
    GO
    
    alter procedure procedureName 
    ...
    

    From the docs:

    For stored procedures, SQL Server uses
    the SET ANSI_NULLS setting value from
    the initial creation time of the
    stored procedure. Whenever the stored
    procedure is subsequently executed,
    the setting of SET ANSI_NULLS is
    restored to its originally used value
    and takes effect. When invoked inside
    a stored procedure, the setting of SET
    ANSI_NULLS is not changed.

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

Sidebar

Related Questions

Synopsis : When calling an executable that links to shared libraries from Java code
Suggestions other that dumping IE6 that is... I can't control that :) Quick synopsis
Synopsis: Each User account has a UserProfile to hold extended info like phone numbers,
Brief synopsis, this code works beautifully and does what it is supposed except with
Is there an OSS which can compress a text to a synopsis? My goal
How can you pick pages from a PDF file? Pseudo-code synopsis pick-pages 1,2-69,70-73,100 example.pdf
I would like to translate the following Unix 1 Liner to PowerShell. Synopsis of
A = imread(filename, fmt) [X, map] = imread(...) The above is in the synopsis
What I want to accomplish: print duplicated lines This is what uniq man says:
I'm trying to read through the documentation on Berkeley DB XML , and I

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.