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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:55:22+00:00 2026-05-31T02:55:22+00:00

I have a table (CheckNumber) with this data: ID NumberFrom NumberTo StateID 1 1

  • 0

I have a table (CheckNumber) with this data:

ID  NumberFrom NumberTo StateID
1   1          10       1
2   2          3        2

This numbers represents physical documents and its states (1 is usable and 2 is unusuable)
So now I want to create a document and want to know what number the document will have and what nummbers can be used.

So I have created this cursor.

Declare @numberUsage as table(accountID int,Number Int)
Declare @bankID int
Declare @stateID int
Declare @beginNumber int
Declare @endNumber int
Declare cCursor cursor for Select accountID, stateID, beginNumber,endNumber From Finances.CheckNumber
Open cCursor
Fetch cCursor Into @bankID,@stateID,@beginNumber,@endNumber
While @@FETCH_STATUS = 0
Begin
    Declare @actual int
    Set @actual = @beginNumber
    If @stateID = 1
    Begin
        While @actual <= @endNumber
        Begin
            if not exists(Select Number From Finances.CheckNPayment Where accountID = @bankID and Number = @actual)
            Begin
                Insert Into @numberUsage values(@bankID,@actual)
            End
            Set @actual = @actual + 1
        End
    End
    Else
    Begin
        While @actual <= @endNumber
        Begin
            Delete From @numberUsage Where accountID = @bankID And Number = @actual
            Set @actual = @actual + 1
        End
    End
    Fetch cCursor Into @bankID,@stateID,@beginNumber,@endNumber
End
Close cCursor
Deallocate cCursor
Select * From @numberUsage

The result is this:

accountID Number
1         1
1         4
1         5
1         6
1         7
1         8
1         9
1         10

Is it possible to convert the table result to a view?

  • 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-31T02:55:24+00:00Added an answer on May 31, 2026 at 2:55 am

    This will take a few steps, but it’s possible. I’ll try to explain each piece:

    Get a list of sequential numbers:

    select row_number() over (order by name) as RN from master..spt_values
    

    If you need more than that, you can cross join to itself and you’ll see a lot wider range. Now, you can use this to get your usable range.

    ;with Nums as (
         select row_number() over (order by name) as RN from master..spt_values)
    select *
    from Nums n
    inner join CheckNumber cn
      on n.RN between cn.NumberFrom and cn.NumberTo
    

    The “with as ()” is called a Common Table Expression (CTE). The link has more info on how it works.

    Now we just need to exclude the unusable items. We can use the same idea to generate a list of unusable numbers and then use the EXCEPT keyword.

    ;with Nums as (
         select row_number() over (order by name) as RN from master..spt_values)
    select n.RN
    from Nums n
    inner join CheckNumber cn
      on n.RN between cn.NumberFrom and cn.NumberTo
    where cn.StateId = 1
    EXCEPT
    select n.RN
    from Nums n
    inner join CheckNumber cn
      on n.RN between cn.NumberFrom and cn.NumberTo
    where cn.StateId = 2
    

    You should be able to adapt this query to your actual data structure and data set fairly easily.

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

Sidebar

Related Questions

I have table data like this id id1 name 1 1 test1 1 1
I have table rows of data in html being filled from a CGI application.
I have table with some fields that the value will be 1 0. This
I have table in data base name train delay, with columns train number(int), DelayTime(int),
I have table with Persian data and utf8_general_ci collection and with php program i
I have table with this structure ID | Parameter1 | Parameter 2 | Multiplication
I have table with statistical data. I'm trying to count the records grouped by
I have table called Direccion other called Cliente each one defined like this public
I have table field defined as like this approved_at DATETIME YEAR TO SECOND NOT
I have table in an MS Access database that looks like this: ID Symbol

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.