How to generate a range of numbers between two numbers?

后端 未结 30 1876
执念已碎
执念已碎 2020-11-22 10:16

I have two numbers as input from the user, like for example 1000 and 1050.

How do I generate the numbers between these two numbers, using

30条回答
  •  渐次进展
    2020-11-22 10:31

    I made the below function after reading this thread. Simple and fast:

    go
    create function numbers(@begin int, @len int)
    returns table as return
    with d as (
        select 1 v from (values(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) d(v)
    )
    select top (@len) @begin -1 + row_number() over(order by (select null)) v
    from d d0
    cross join d d1
    cross join d d2
    cross join d d3
    cross join d d4
    cross join d d5
    cross join d d6
    cross join d d7
    go
    
    select * from numbers(987654321,500000)
    

提交回复
热议问题