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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:45:36+00:00 2026-05-20T19:45:36+00:00

This question is a followup to a previous question I had about discovering unused

  • 0

This question is a followup to a previous question I had about discovering unused sequential number ranges without having to resort to cursors (Working with sequential numbers in SQL Server 2005 without cursors). I’m using SQL Server 2005.

What I need to do with those numbers is to assign those numbers to records in a table. I just can’t seem to come up with a way to actually relate the numbers table with the records that need those numbers.

One possible solution that came to mind was insert the records in a temp table using an identity and using the beginning of the number range as an identity seed. The only problem with this approach is that if there are gaps in the number sequence then I’ll end up with duplicate control numbers.

This is how my tables look like (overly simplified):

Numbers table:

Number      
-------
102314
102315
102319
102320
102324
102329 

Data table:

CustomerId   PaymentAmt   ControlNumber
----------   ----------   -------------
1001         4502.01      NULL
1002         890.00       NULL
9830         902923.34    NULL

I need a way to make it so i end up with:

CustomerId   PaymentAmt   ControlNumber
----------   ----------   -------------
1001         4502.01      102314
1002         890.00       102315
9830         902923.34    102319

Is this possible without having to use cursors? The reason I’m avoiding cursors is because our current implementation uses cursors and since its so slow (8 minutes over 12,000 records) I was looking for alternatives.

Note: Thanks to all who posted answers. All of them were great, I had to pick the one that seemed easier to implement and easiest to maintain for whomever comes after me. Much appreciated.

  • 1 1 Answer
  • 4 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-20T19:45:37+00:00Added an answer on May 20, 2026 at 7:45 pm

    Try this:

    ;WITH CTE AS
    (
        SELECT *, ROW_NUMBER() OVER(ORDER BY CustomerId) Corr
        FROM DataTable
    )
    
    UPDATE CTE
    SET CTE.ControlNumber = B.Number
    FROM CTE
    JOIN (  SELECT Number, ROW_NUMBER() OVER(ORDER BY Number) Corr
            FROM NumberTable) B
    ON CTE.Corr = B.Corr
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a followup of a previous question I had. I got the very
This is a follow up to a previous question that I had before about
This is a follow-up to a previous question I had about interfaces. I received
This is a followup on the question: ASP.NET next/previous buttons to display single row
This is a follow up question to a previous question I asked about calculating
This is a follow-up on a previous question I had ( Complexity of STL
This is somewhat of a followup to my previous question , where people pointed
Hey guys this is a followup to my previous question . I now have
I suppose this question is a follow up to a previous question I had
A followup to this previous question: Current code: var query = from b in

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.