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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:08:43+00:00 2026-05-27T02:08:43+00:00

(This is a follow-up to the answer by onedaywhen on a question I posted

  • 0

(This is a follow-up to the answer by “onedaywhen” on a question I posted earlier today.)

Hi everyone. Say I have a table MyTable with two int fields, PrimaryKey and MyNumber. (And other fields not directly relevant to this question). MyNumber has a unique constraint and a check constraint limiting it to BETWEEN 1 AND n. (Let’s say n=5 for now.)

1,2  
2,NULL  
3,5  
4,NULL  
5,NULL  
6,1  
7,NULL

How could an UPDATE be written to change the record where PrimaryKey=2 so that MyNumber has a non-NULL value? I don’t care what value it has, so long as it’s not a NULL and meets it’s two constraints of being unique and within range.

I’m using MS SQL Server, but I’m hoping there’s an answer using standard SQL.

(I’m also hoping there won’t have to be a table with the numbers 1 to n as contents.)

Many thanks.

  • 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-27T02:08:44+00:00Added an answer on May 27, 2026 at 2:08 am
    WITH CTE AS (
        SELECT 1 N
        UNION ALL
        SELECT N + 1 FROM CTE WHERE N < 5
    )
    UPDATE MyTable
    SET MyNumber = (
        SELECT TOP 1 N FROM CTE
        WHERE NOT EXISTS (SELECT * FROM MyTable WHERE MyNumber = N)
    )
    WHERE PrimaryKey = 2
    

    In plain English:

    • Generate integers between 1 and 5 (WITH CTE AS ...).
    • Pick the first of these integers that does not already exist in MyNumber (SELECT TOP 1 ...).
    • Assign that integer to MyNumber, in the row identified by PrimaryKey = 2 (UPDATE ...).

    If this query fails to find a suitable value, it will simply set the MyNumber to NULL.


    WARNING: This might still viloate UNIQUE constraint on MyNumber in a concurrent environment (i.e. when two concurrently-executing transactions try to run this same query in parallel). So, you’d have to be prepared to retry the query if necessary.

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

Sidebar

Related Questions

This is a follow-up to this question's answer . How can I modify the
This is a follow-up question to an answer I gave a few days back
This is a follow up question to the selected answer in this post: Output
I have a number of xml files that should follow this format: <root> <question>What
This is a follow up question to this answer: How do I parse XML
This is a follow up to James' answer to this question: Flattening iterator I
This is a follow-up question to my previous question. As suggested in this answer
Note: This is a follow-up to an answer on a previous question . I'm
This is a follow up question from here specifically concerning its answer . From
This is a follow up to a previous question; I got an answer 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.