应楼主要求给出代码:
---数据准备
create table b
(
SName varchar(10),
SText varchar(10),
SNum int
)
insert into b
select 'a','aa',1
union
select 'b','bb',2
union
select 'c','cc',3
union
select 'd','dd',4
union
select 'e','ee',5
union
select 'f','ff',6
union
select 'g','gg',7
union
select 'h','hh',8
union
select 'i','ii',9
---存储过程的代码
Create Proc pr_GetRandomRecords
as
Declare @Max money,@Min money,@Random float
select @Max = max(Snum),@Min = min(Snum) from b
select *,SFactor = (Snum - @min)/ (@max - @min) into #a from b
set @Random = rand()
Select * into #b from #a where Sfactor >= @random
Select top 1 *,[id] = newid() from #b order by newid()
Go
----运行存储过程得到的结果
exec pr_GetRandomRecords
结果:
(9 row(s) affected)
(2 row(s) affected)
SName SText SNum SFactor id
---------- ---------- ----------- --------------------- ------------------------------------
h hh 8 .8750 BC400AA3-C35C-436D-A427-4104E9BA2A92
(1 row(s) affected)