In SQL Server, it is often desirable to have a dynamic variable determine the number of rows returned by a query. Unfortunately, the following syntax is not valid in SQL Server 2005 and later:
DECLARE @count int SET @count = 20 SELECT TOP @count * FROM SomeTable
To achieve the desired functionality, use the following syntax:
SELECT TOP (@count) * FROM SomeTable
This syntax dynamically determines the number of rows to return based on the value of the @count variable. It is supported in SQL Server 2005 and later.
The above is the detailed content of How Can I Use a Variable to Dynamically Set the Number of Rows Returned by SELECT TOP in SQL Server?. For more information, please follow other related articles on the PHP Chinese website!