22,210
社区成员
发帖
与我相关
我的任务
分享
DECLARE @ID INT ,
@Count INT
SELECT @ID = 5000 ,
@Count = 100;
WITH a1
AS ( SELECT @ID id ,
1 n
UNION ALL
SELECT @ID - n ,
n + 1
FROM a1
WHERE n < @Count
)
SELECT id
FROM a1
DECLARE @ID INT ,
@Count INT
SELECT @ID = 5000 ,
@Count = 100;
WITH a1
AS ( SELECT @ID id ,
1 n
UNION ALL
SELECT @ID - n ,
n + 1
FROM a1
WHERE n < @Count
)
SELECT id
FROM a1
declare @id int
declare @count int
declare @tb table(id int,c int)
insert into @tb
select 5000,4 union all
select 4999,4 union all
select 4998,4 union all
select 4997,4
select v.*
from @tb v,master..spt_values t
where t.type = 'P' and t.number > 0 and t.number <= v.c
/*
id c
5000 4
5000 4
5000 4
5000 4
4999 4
4999 4
4999 4
4999 4
4998 4
4998 4
4998 4
4998 4
4997 4
4997 4
4997 4
4997 4
*/
--测试数据
if object_id('t') is not null drop table t
go
create table t([ID] int)
declare @n int, @ID int, @Count int
set @ID=5000
set @Count=4
set @n=0
--循环插入
while @n<@Count
begin
insert t
select @ID-@n
select @n=@n+1
end
select * from t
drop table t
(1 行受影响)
(1 行受影响)
(1 行受影响)
(1 行受影响)
ID
-----------
5000
4999
4998
4997declare @id int
declare @count int
select @ID=5000,@Count=4
select v.*
from
(
select @id id,@count c
)v,master..spt_values t
where t.type = 'P' and t.number > 0 and t.number <= v.c
/*
id c
5000 4
5000 4
5000 4
5000 4
*/
CREATE TABLE #temp(ID INT)
DECLARE @ID INT,@Count int
select @ID=5000,@Count=4
;WITH a1 AS
(
SELECT @ID id,1 n
UNION ALL
SELECT ID-1,n+1 FROM a1
WHERE n<@Count
)
INSERT #temp
SELECT id FROM a1
SELECT * FROM #temp