How to use LIMIT keyword in SQL Server 2005?

后端 未结 3 674
忘掉有多难
忘掉有多难 2020-12-15 04:01

I have found a way to select random rows from a table in this post. A suggestion is to use the following query:

SELECT * FROM employee ORDER BY RAND() LIMIT          


        
相关标签:
3条回答
  • 2020-12-15 04:42

    If you take a look at the SELECT statement in SQL Server Books Online, then you'll see that you can limit the resultset by using the TOP keyword.

    SELECT TOP 1 * FROM employee
    
    0 讨论(0)
  • 2020-12-15 04:45

    I'm using this fairly simple one (SQL2005) to limit the number of rows returned, which will work with a value provided by a stored procedure parameter as well.

    DECLARE @Limit int
    SET @Limit = 10
    SELECT TOP (@Limit) Col1, Col2 FROM SomeTable
    
    0 讨论(0)
  • 2020-12-15 04:53
    SELECT TOP 1 * FROM Employee ORDER BY newid()
    

    You have to use newid() for it to be evaluated once per row.

    0 讨论(0)
提交回复
热议问题