First: What do you think about the way I random? Is this way too poor?.
I have a table TABLE_A:
id | name | state
---------------------|----------
1703248 | blablabla | 1
... | blablabla | 0
7873415 | blablabla | 1
7926033 | blablabla | 1
The id column can contain alfanumeric values too, like 'zxg-76354GH-34347' for example. Well, I have 3060 records.
Detail:
select count(1) from TABLE_A where id like 'n%'
if:
n=1 ---> 201 records , n=2 ---> 147 records , n=3 ---> 187 records
n=4 ---> 327 records , n=5 ---> 430 records , n=6 ---> 503 records
n=7 ---> 1175 records , n=8 ---> 35 records , n=9 ---> 55 records
My stored procedure that randomize:
--Name: Randomize_sp
BEGIN
DECLARE @temp table(id varchar(50))
--RANDOM 1
INSERT INTO @temp
SELECT id FROM TABLE_A
WHERE state > 0
ORDER BY RAND(CHECKSUM(NEWID()))
--RANDOM 2
SELECT top 1 id FROM @temp ORDER BY RAND(CHECKSUM(NEWID()))
END
I call that stored procedure many times or 'n' times(I need to do this):
BEGIN
DECLARE @nTimes int, @i int
DECLARE @tempT table(id varchar(50))
SET @nTimes = 12
SET @i = 0
WHILE @i < @nTimes
BEGIN
INSERT INTO @tempT
EXECUTE MyDB.dbo.Randomize_sp
SET @i = @i + 1
END
SELECT id FROM @tempT
END
The problem is: always I get 4 or 5 rows that begins with '7', sometimes I get 3 or 2 rows that begins with '5'...I'd like to avoid repetitions. I apreciate any suggestion.
Thanks.