create proc 过程名
@间隔数 int
as
declare @tem int
select IDENTITY(int,1,1) AS ID_Num,* into #temp from test
set @tem=@@rowcount
set @tem=cast(rand(checksum(newid()))*@tem as int)
select * from #temp where ID_Num between @tem and @tem+@间隔数
go
@间隔数 int
as
declare @tem int
select IDENTITY(int,1,1) AS ID_Num,* into #temp from test
set @tem=@@rowcount
set @tem=cast(rand(checksum(newid()))*@tem as int)
select * from #temp where ID_Num between @tem and @tem+@间隔数
go
declare @currentid int,@step int
--生成临时表
select myid=identity(int,0,1),* into #temp from test--随机取得从第N条记录开始
select @currentid=myid from #temp order by newid()--设置记录间隔,每隔27条记录显示一条
set @step=27--显示结果
select * from #temp where myid >=@currentid and ((myid-@currentid) % @step)=0--删除临时,如果在存储过程中使用,可不用手工删除临时表
drop table #aa
as
declare @tem int
select IDENTITY(int,1,1) AS ID_Num,* into #temp from test
set @tem=@@rowcount
select * from #temp where ID_Num > @开始数
and (id_Num-@开始数)%(@间隔数)=0
go
不会真是你同学吧?你是那里的,看有没有可能?
declare @begin int ,@interval int
set @interval=25select top 1 @begin=id from table order by newid()select * from table
where (id-@begin)%6=0
--循环取一遍数据,只需要从@begin开始 + and id>=@begin
declare @b int
declare @s int
declare @i int
declare @j int
set @s = 25 /**设步长为25**/
set @i = 1
select top 1 @b = id from #t order by newid()
update #t set flag = @i where id = @b
while exists (select * from #t where flag = 0)
begin
set @j = 0
while @j < @s
begin
if @b = 300
set @b = 1
else
set @b = @b + 1
if exists (select 1 from #t where id = @b and flag = 0)
set @j = @j + 1
end
set @i = @i + 1
update #t set flag = @i where id = @b
end
select id from #t order by flag
1、pengdali(大力 V2.0)力哥一派的,测试结果为:
随机数取得的记录为38开始的,然后依次取到的记录是38/39/40/41/42。/63
即变成了取从随机数后面的25个数。
2、zjcxc(邹建)建哥一族的,测试结果为:
随机数取得的记录为38开始的,然后依次取到的记录是63/88/113/138/163。/288
附合我操作需求中的:
从这个随机数的位置开始,根据前台输入的一个间隔数来依次抽取记录。
可惜却没有循环,即当288+25=313(已经大于300)时不能将指针指回1,将小于63且间隔数为25的取出。
即正确的结果应为
63/88/113/138/163。/288/25/50
这样取才对。
3、tj_dns(愉快的登山者)在题目意图的理解上是完全正确的,可是我用你的方法
测试了近一个小时了也没结果出来。估计是死循环了吧。看来这题有难度啊。我要再放些分给各位了。
as
declare @tem int
select IDENTITY(int,1,1) AS ID_Num,* into #temp from test
set @tem=@@rowcount
select * from #temp where ID_Num > @开始数
and (id_Num-@开始数)%(@间隔数)=0
go我的难道不行吗?
select * from #temp where myid >=@currentid and ((myid-@currentid) % @step)=0
union
select * from #temp_1 where (myid>0 and myid<@currentid)and((myid-0) % @step)=0即可完全正确了。呵呵,谢谢各位了。准备结帐了。
select * from #temp where myid >=@currentid and ((myid-@currentid) % @step)=0
union
select * from #temp where (myid>0 and myid<@currentid)and((myid-0) % @step)=0
你的结果和zjcxc(邹建)一样。不过你忘了题目需求中的第一条。
(存储过程还要完成取随机数的工作)
力哥的
set @tem=@@rowcount
set @tem=cast(rand(checksum(newid()))*@tem as int)
与
select @currentid=myid from #temp order by newid()
方法好象都可以,有区别吗?几位师兄能解释一下吗?
newid()这个函数在联机帮助中的解释看得不怎么懂啊。