This is my code: the code returns a random number all right but when I put TOP it does not generate 5 rows, it always generates a row
declare @NumeroDesde int = 00 ;
declare @NumeroHasta int = 99 ;
SELECT top 5 ROUND(((@NumeroHasta - @NumeroDesde) * RAND() + @NumeroDesde), 0)
help thanks in advance
The problem has two parts.
A first that is to generate the number of rows that one wants, and a second, that the values of those rows are random and different from each other.
For the first part, you can use Itzik Ben-Gan's getNums function
It returns the list of numbers within the range supplied as parameters. In the case at hand it would be a 1 and a 5. And it returns 1,2,3,4,5.
And the second part is getting these random ones.
For this you can use the newid() functions that return a uniqueidentifier value. The checksum() function that will return an int of the supplied parameter. And abs() that will return the positive absolute value. The module 100 will return the remainder of dividing the number by 100, so it will be a number between 0 and 99, and there is no need to define the variables.
Random numbers in a range