This procedure generates random unique numbers between two numbers. It gets 3 parameters. The first parameter is number you want to list, the second parameter is the start value and the last parameter is the end value.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

Create procedure randomGenerate ( @ unit int , @ min int , @ max int )

as

Begin

Declare @ numbers table ( number int )

Declare @ i int = 0

Declare @ number int

while ( @ i < @ unit )

Begin

Set @ number = floor ( rand ( ) * ( @ max - @ min + 1 ) ) + @ min

if ( not exists ( Select * from @ numbers where number = @ number ) )

begin

insert into @ numbers values ( @ number )

Set @ i = @ i + 1

end

end

Select * from @ numbers order by 1

End

After you run the code above than you run the code below.

Execute randomGenerate 5 , 20 , 30

Result

Lists 5 numbers between 20 and 30

Execute randomGenerate 6 , 0 , 49

Result

Lists 6 numbers between 0 and 49

Execute randomGenerate 10 , 50 , 100

Result

Lists 10 numbers between 50and 100

To See Other Stored Procedure Examples Click

YOU MAY WANT TO SEE OUR ALL EXAMPLES PAGE, THEN CLICK HERE

Like this: Like Loading...

Related