Pages

Social Icons

Wednesday 16 May 2012

Get random values in TOP predicate using NEWID()

In SQL-Server we have NEWID() function which will give us a new id every time we can see this by executing the below statement.
       SELECT NEWID()


We can use this function in our order by clause while using the TOP Predicate.


Example : 


 SELECT TOP(3)
        EmployeeName,
        Salary
 FROM 
Employee
 ORDER BY
NEWID()


This statement will give us every time different TOP 3 records.


Thx,
RS
 



No comments:

Post a Comment