ASP/SQL让一个列能在规定的数字范围内递增或随机生成不重复的数字
ASP/SQL让一个列能在规定的数字范围内递增或随机生成不重复的数字
如范围是100-1000之间
id | name | number |
1 abc 101
2 aaa 266
3 dsf 999
4 sdf 500
5 dfd 328
最好是在一定范围内随机生成,如果不行,递增也可以。是在SQL执行,还是在程序中执行。怎么做?
[解决办法]
'参数说明:c_floor随机数的下限,c_upper随机数的上限,num要生成随机数的个数'注意:c_floor<c_upper而且都不能为负数,num<=c_upper-c_floor,否则会出现死循环'调用 call random(1,40,10) 随机产生10个1-40之间的数字 Function random(c_floor,c_upper,num) Dim a(),temp,flag,i,j Redim a(num) '重定义数组 i=1 Do while i<cint(num)+1 '生成随机数 randomize() temp=cint(int((cint(c_upper)*rnd())+cint(c_floor))) flag=0 '判断生成的随机数时候已经存在,是则重新生成 for j=1 to ubound(a) if cint(temp)=cint(a(j)) then flag=1 Exit for End if next '将生成的随机数存放到数组中 if flag=0 then a(i)=temp Response.write a(i) & "<br>" i=i+1 End if loop random=a '返回一个数组对象 End function
[解决办法]
rand() 返回从 0 到 1 之间的随机 float 值。select ceiling(rand()*1000)
[解决办法]
create table t(id int ,name varchar(3),number int)insert t select1, 'abc' , 101 union all select 2 , 'aaa' , 266 union all select3 , 'dsf' , 999 union all select4 , 'sdf' , 500 union all select5 , 'dfd', 328 alter table t add nn intdeclare @n int,@m intset @n=1set @m=(select max(id)from t)while @n<=@mbegin update t set nn= ceiling(rand()*1000) where id=@n set @n=@n+1endgo select * from tid name number nn----------- ---- ----------- -----------1 abc 101 9882 aaa 266 9523 dsf 999 4194 sdf 500 1975 dfd 328 167(5 行受影响)drop table t
[解决办法]
随机和区间的简单 递增不重复的需要做特别处理
写个大概的
<%
Function RndNumber(MaxNum,MinNum)
Randomize
RndNumber=int((MaxNum-MinNum+1)*rnd+MinNum)
RndNumber=RndNumber
End Function
%>
如果需要小数把int去掉
递增和重复问题只能判断数据表中现有数据了
用select查吧
[解决办法]
create table tb(id int,name varchar(10)) insert into tb values(1 , 'abc')insert into tb values(2 , 'aaa')insert into tb values(3 , 'dsf')insert into tb values(4 , 'sdf')insert into tb values(5 , 'dfd')go--sql 2000select t.* , number = (select count(1) from tb where id < t.id) + 100 from tb tselect t.* , number = (select count(1) from tb where id < t.id) + 101 from tb t--sql 2005select t.* , number = ROW_NUMBER() OVER(ORDER BY id) + 100 from tb tselect t.* , number = ROW_NUMBER() OVER(ORDER BY id) + 101 from tb tdrop table tb
[解决办法]
之前用SQL2005做过一个生成指定不重复随机数的脚本,生成随机数比较简单,用rand()函数就能实现,不重复的话,有几种方案,1.先用EXISTS判断,没有重复就INSERT,2.先INSERT,如果有ERROR就返回重新生成,3.TRY/CATCH,将INSERT的部分放到TRY,重新生成随机数的部分放到CATCH
这三种方案都能实现,比较下,EXISTS方案效率低,但通用性高,ERROR方案在页面会中止,但SQL里不会,但效率最高,TRY/CATCH方案效率居中,但只能在SQL2005实现,SQL2000不支持TRY/CATCH,具体选哪种,楼主自己决定。
[解决办法]
declare @yourTable table (number int)--假设这是你的表,n为insert into @yourTable values(9)insert into @yourTable values(6)insert into @yourTable values(5)insert into @yourTable values(4)insert into @yourTable values(2)insert into @yourTable values(3)insert into @yourTable values(1)declare @from int,@to intselect @from = 1,@to = 10--设置范围declare @i int set @i = 0declare @tb table(idx int,nb int)set @from=@from-1insert into @tb(nb)select top (@to-@from) row_number()over(order by @from)+ @from from syscolumns,sysobjectsdelete @tbfrom @yourTable twhere nb = t.numberupdate @tb set idx = @i,@i = @i+1select nb from @tb where idx = ceiling(rand()*@i)