SQL: How do I use parameter for TOP like in SELECT TOP @amount? [duplicate] SQL: How do I use parameter for TOP like in SELECT TOP @amount? [duplicate] sql-server sql-server

SQL: How do I use parameter for TOP like in SELECT TOP @amount? [duplicate]


Need parenthesis, and only for SQL Server 2005 and above

SELECT TOP (@param1) ...


For older versions of SQL Server, you can use:

SET ROWCOUNT @NumberOfResultsSELECT * FROM MyTableSET ROWCOUNT 0

However, you should not use this technique on 2008:

Using SET ROWCOUNT will not affect DELETE, INSERT, and UPDATE statements in the next release of SQL Server (2008). Do not use SET ROWCOUNT with DELETE, INSERT, and UPDATE statements in new development work, and plan to modify applications that currently use it. Also, for DELETE, INSERT, and UPDATE statements that currently use SET ROWCOUNT, we recommend that you rewrite them to use the TOP syntax. For more information, see DELETE (Transact-SQL), INSERT (Transact-SQL), or UPDATE (Transact-SQL).