-1

I want to generate a random number against StudentID, I am using the following SQL enter image description here

The Result i am getting is :

enter image description here

Please Help.

Community
  • 1
  • 1
JamshaidRiaz
  • 111
  • 7
  • What's wrong with the result you're getting? - What do you *really* want to achieve? – JimmyB Apr 28 '15 at 15:13
  • See the RNO_Code field it should be unique random number but its 1 for all rows – JamshaidRiaz Apr 28 '15 at 15:15
  • Take look at [this answer](http://stackoverflow.com/a/849888/4519059) ;). – shA.t Apr 28 '15 at 15:15
  • If STUDKEY is unique, then you're getting exactly the results you should expect to get. Why don't you use the RAND() function? http://www.sql-server-helper.com/tips/generate-random-numbers.aspx – Tab Alleman Apr 28 '15 at 15:15
  • "Unique" is an important restriction here. I doubt that's possible in a single statement. – JimmyB Apr 28 '15 at 15:16
  • possible duplicate of [How do I generate random number for each row in a TSQL Select?](http://stackoverflow.com/questions/1045138/how-do-i-generate-random-number-for-each-row-in-a-tsql-select) – JimmyB Apr 28 '15 at 15:16

1 Answers1

0

You're just setting that column to the column index of the partitioned row_number, which is 1. If you want a random number for each row, it would be easier to do this:

UPDATE TAB_EXAM_FORMS
SET RNO_CODE = CAST(RAND(CHECKSUM(NEWID())) * 10 AS INT) + 1

Just change the multiplication to whatever range you want. This range is 1-10.

John Bell
  • 2,350
  • 1
  • 14
  • 23