Click here to Skip to main content
15,886,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to update a Table Column with shuffled numbers from a given range. I have worked on the following query but it is generating DUPLICATE numbers. I need help to remove duplicates.

Please help. I am using SQL-server-2008

SQL
UPDATE TAB_EXAM_FORMS
SET
	RNO_CODE = CAST(RAND(CHECKSUM(NEWID())) * 10 AS INT)+1 
	WHERE  SESSION_ID='46'  
.

Please note 10 is the range in the above query. and Session_Id is the filter for the rows of the table.

Thanks and Regards
Posted

1 solution

If you use a SQL Server Management Tool and run the query
SQL
select CAST(RAND(CHECKSUM(NEWID())) * 10 AS INT)+1 

about 20 times you could simply see the result is a random value between 1 and 10. So it is very possible you get duplicates.
Enlarge your range or only use the NewID().

If you would do:
SQL
DECLARE @generatedValue INT
SET @generatedValue = CAST(RAND(CHECKSUM(NEWID())) * 10 AS INT)+1

You have a variable which you can test for.
But why you have to remove duplicate number if you use an UPDATE statement. Is there somewhere an INSERT statement available in your code that causes the problem?
 
Share this answer
 
v2
Comments
Jamshaid Riaz 30-Apr-15 13:15pm    
Dear Permalink, actually the numbrs i m trying to generat are secret code for each examinee in a exam and they must not be duplicate.
Herman<T>.Instance 30-Apr-15 13:51pm    
set range to 25000 or so

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900