/*************************************************************************
* Title : How to
select a row from a table randomly
* Solution : We can get
the random rows by using the NEWID() function that
generates
global unique identities in random orders
*************************************************************************/
--Creating a table
CREATE TABLE Student
(RollNo INT,
Name VARCHAR(100)
)
--Inserting records into the table
INSERT INTO Student(RollNo,Name) VALUES (1,'Rahul')
INSERT INTO Student(RollNo,Name) VALUES (2,'Vijaya')
INSERT INTO Student(RollNo,Name) VALUES (3,'Vinod')
INSERT INTO Student(RollNo,Name) VALUES (4,'Sachin')
INSERT INTO Student(RollNo,Name) VALUES (5,'Savilash')
INSERT INTO Student(RollNo,Name) VALUES (6,'Vishal')
INSERT INTO Student(RollNo,Name) VALUES (7,'Abhinav')
INSERT INTO Student(RollNo,Name) VALUES (8,'Anubha')
INSERT INTO Student(RollNo,Name) VALUES (9,'Nishant')
INSERT INTO Student(RollNo,Name) VALUES (10,'Ramya')
--Checking the records
SELECT * FROM Student
--Execute the select statement multiple times & get the name of the students randomly
SELECT TOP 1
RollNo,
Name
FROM Student
ORDER BY NEWID()
/***************End of the Script**********/
No comments:
Post a Comment