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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:42:57+00:00 2026-05-30T12:42:57+00:00

When reviewing code I came across something odd, someone had read that you can

  • 0

When reviewing code I came across something odd, someone had read that you can use ABS(CHECKSUM(NewId())) % N to get you random numbers from 0 to N-1 (as RAND() doesn’t occur per-row), but I suspect they didn’t really test their code (simplified down now with table variables, and actual code did a TOP 1 country to incorrectly get around a problem below):

DECLARE @Values TABLE
(
    id int identity,
    country VARCHAR(100)
)

INSERT INTO @Values (country) VALUES ('UK'), ('USA'), ('China')

SELECT *, (SELECT country FROM @Values v WHERE v.id = ABS(CHECKSUM(NewId())) % 3 + 1)
FROM @Values

When executed the following error occurs:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

My first question was how can the subquery return more than one value? and how is it that it some times returns no value at all? (Note not the question that I’m asking at SO) Tested by:

SELECT country FROM @Values v WHERE v.id = ABS(CHECKSUM(NewId())) % 3 + 1

Yet no matter how many times the following is executed:

SELECT ABS(CHECKSUM(NewId())) % 3 + 1

The returned results are always 1,2,3 (this was the ‘testing’ the programmer had used when writing their code)

From all this, I suspect this is because it’s being re-executed for every compare and not every row, can someone confirm this and provide a good link to explain this behaviour so that I can point it out to him?

  • 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-30T12:42:59+00:00Added an answer on May 30, 2026 at 12:42 pm

    Why, this is expected.

    ABS(CHECKSUM(NewId())) % 3 + 1 is evaluated once for each row in @Values, and therefore has different values for each row.

    So it can return multiple rows, or no rows at all.

    This is exactly what you should be expecting when saying, This expression evaluates for every row.

    Apparently what you actually want is an expression that evaluates once per query.

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

Sidebar

Related Questions

I recall reviewing someone else's PHP code once and he had a function or
I am reviewing some code at work and came across an inconsistency in how
I was reviewing some code and I came across this: public static doSomething(String myString,
I'm reviewing some code from my job from another employee and came across a
Is there a compiler setting that can warn about these? Currently I'm reviewing code
I was reviewing some code today and came across some code (accurately portrayed by
I was reviewing some code for log4net and I came across this. private static
I was recently reviewing some Javascript code in a jQuery plug-in and came across
I am reviewing an example code in a book and came across the following
Whilst reviewing some Qt C++ code I came across this: class Foo { Q_OBJECT

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.