Here is a stored procedure that will generate a series of random numbers and return them in a result set.
/*
This SQL Server stored procedure returns a result set
containing a series of random integers
Inputs are:
@begin_no - lowest number to be generated,
@end_no - highest number to be generated
Calling syntax: Exec sp_GenerateRandomNumberResultSet Begin, End
Created by Terry L. Broadbent, Oct 3 2001
Please inform me of any bugs encountered. Comments and suggestions for improvement are encouraged.
*/
[tt]
CREATE procedure sp_GenerateRandomNumberResultSet
@begin_no int=1, @end_no int=100 As
Set nocount on
Declare @rn int, @rec int, @cnt int
Set @cnt=@end_no-@begin_no+1
Create Table #t (RecNo int identity(1,1), RandNo int null)
Set @rec=1
While @rec<=@cnt
Begin
Insert Into #t (RandNo) Values (null)
Set @rec=@rec+1
End
Set @rec=1
While @rec<=@cnt
Begin
Set @rn=rand()*@cnt+@begin_no
If @begin_no<0 Set @rn=@rn-1
Update #t Set RandNo=@rn
Where RecNo=@rec
And @rn Not In
(Select RandNo
From #t Where RandNo Is Not Null)
If @@rowcount>0 Set @rec=@rec+1
End
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.