- 简介
- 用while产生
- 用with产生
简介
在SQL中,要产生连续的自然数,有很多方法。本文暂时列举了其中的两种方法,后续发现更多方法再补充。
用while产生
用while产生连续的自然数,只要控制好while条件就好了
declare @i int = 1
declare @count int = 100
while @i <= @count
begin
print @i
set @i = @i + 1
end
用with产生
用with产生连续的自然数,主要是with可以引用自身
declare @i int = 1
declare @j int = 1
declare @count int = 100
;WITH q(num) as (
select 1
union all
select num + 1
from q
where num < @count
)
select * from q