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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:33:46+00:00 2026-05-15T22:33:46+00:00

In my code, I need to test whether specified column is null and the

  • 0

In my code, I need to test whether specified column is null and the most close to 0 as possible (it can holds numbers from 0 to 50) so I have tried the code below.
It should start from 0 and for each value test the query. When @Results gets null, it should return. However, it does not work. Still prints 0.

declare @hold int
declare @Result int
set @hold0
set @Result=0

WHILE (@Result!=null)
BEGIN
select @Result=(SELECT Hold from Numbers WHERE Name='Test' AND Hold=@hold)
set @hold=@hold+1
END

print @hold
  • 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-15T22:33:47+00:00Added an answer on May 15, 2026 at 10:33 pm

    First, you can’t test equality of NULL. NULL means an unknown value, so you don’t know whether or not it does (or does not) equal any specific value. Instead of @Result!=NULL use @result IS NOT NULL

    Second, don’t use this kind of sequential processing in SQL if you can at all help it. SQL is made to handle sets, not process things sequentially. You could do all of this work with one simple SQL command and it will most likely run faster anyway:

    SELECT
        MIN(hold) + 1
    FROM
        Numbers N1
    WHERE
        N1.name = 'Test' AND
        NOT EXISTS
        (
            SELECT
                *
            FROM
                Numbers N2
            WHERE
                N2.name = 'Test' AND
                N2.hold = N1.hold + 1
        )
    

    The query above basically tells the SQL Server, “Give me the smallest hold value plus 1 (MIN(hold) + 1) in the table Numbers where the name is test (name = ‘Test’) and where the row with name of ‘Test’ and hold of one more that that does not exist (the whole “NOT EXISTS” part)”. In the case of the following rows:

    Name      Hold
    --------  ----
    Test      1
    Test      2
    NotTest   3
    Test      20
    

    SQL Server finds all of the rows with name of “Test” (1, 2, 20) then finds which ones don’t have a row with name = Test and hold = hold + 1. For 1 there is a row with Test, 2 that exists. For Test, 2 there is no Test, 3 so it’s still in the potential results. For Test, 20 there is no Test, 21 so that leaves us with:

    Name      Hold
    --------  ----
    Test      2
    Test      20
    

    Now SQL Server looks for MIN(hold) and gets 2 then it adds 1, so you get 3.

    SQL Server may not perform the operations exactly as I described. The SQL statement tells SQL Server what you’re looking for, but not how to get it. SQL Server has the freedom to use whatever method it determines is the most efficient for getting the answer.

    The key is to always think in terms of sets and how do those sets get put together (through JOINs), filtered (through WHERE conditions or ON conditions within a join, and when necessary, grouped and aggregated (MIN, MAX, AVG, etc.).

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

Sidebar

Related Questions

I have some code in my error handler I need to test against a
I have a code that need to be translated from C to Cpp, and
I need to test whether each number from 1 to 1000 is a multiple
In my code, I need to test whether or not a string is empty
For a test 'crash' I need a small piece of Delphi code to see
I am trying to unit test some code, and I need to to replace
I need code for read json data from url in ios (objective c) If
In the following code I need to print output from the variables $stout ,
I need to write some code to stress-test RS232 communication using rxtx ( note
I know how to check if I have internet access (using the code from

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.