简体   繁体   中英

How to use LIMIT keyword in SQL Server 2005?

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 1

But when I run this query in MS SQL 2005, I get the following error message

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'LIMIT'.

Can anyone tell me where am I wrong? Doesn't MS SQL support LIMIT? If so, then how can I do this?

如果您查看SQL Server联机丛书中的SELECT语句,那么您将看到可以使用TOP关键字限制结果集。

SELECT TOP 1 * FROM employee
SELECT TOP 1 * FROM Employee ORDER BY newid()

你必须使用newid()来每行评估一次。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM