Thursday, March 02, 2006

SELECT TOP N

To return only the top N rows in a SQL select statement, it is possible to parameterize the number N in both SQL Server 2000 and SQL Server 2005; you don't have to select the whole result set into a temporary table and whittle down the rows by yourself.
For SQL2K:
SET ROWCOUNT @NumberOfRows
SELECT * FROM SomeTableOfYourChoice
SET ROWCOUNT 0

and for SQL2K5
SELECT TOP(@NumberOfRows) FROM SomeTableOfYourChoice

1 comment:

Jono said...

Sybase doesn't understand the TOP N syntax, so this is the way a Sybase developer always performs this task.