Take rest :-)

Your quest ends here( konchem .Net, konjam SQL, thoda lifestyle, kurachu movie info)

How to display records of a table in random order in SQL Server

Posted by chandru14 on July 21, 2008

Hello friends,

You might have come across some situations where you need to display the records from a table in a random order. Especially this is a common task for applications which conducts online examinations. 

 

We can handle this situation very easily using a SQL function NewID() which generates a unique value of type uniqueidentifier with every call

 

Example:-

CREATE TABLE Question(Question_id INT, QuestionText VARCHAR(2000))

GO

INSERT INTO Question VALUES(101,‘Tallest building?’)

INSERT INTO Question VALUES(102,‘Longest bridge?’)

INSERT INTO Question VALUES(103,‘Total countries in world?’)

INSERT INTO Question VALUES(104,‘fastest animal?’)

INSERT INTO Question VALUES(105,‘Total no of states in india?’)

INSERT INTO Question VALUES(106,‘abc?’)

INSERT INTO Question VALUES(107,‘xyz?’)

INSERT INTO Question VALUES(108,‘123?’)

INSERT INTO Question VALUES(109,‘mtr?’)

GO

SELECT

           Question_id

          ,QuestionText

          ,NEWID() AS RandomKey

FROM Question

ORDER BY RandomKey

 

Click here to download the script

 

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>