Home >Database >Mysql Tutorial >How to Correctly Use Dynamic TOP Clauses in SQL Server?
Mastering Dynamic TOP Clauses in SQL Server
Dynamically controlling the number of retrieved rows adds significant flexibility to SQL queries. The query structure DECLARE @count int; SET @count = 20; SELECT TOP @count * FROM SomeTable;
is, however, incompatible with SQL Server 2005 and later versions.
The correct approach involves using the parenthesized syntax. The improved query is:
<code class="language-sql">SELECT TOP (@count) * FROM SomeTable</code>
This adjusted syntax adheres to SQL Server 2005 and subsequent versions' requirements. It guarantees the correct interpretation of the dynamic variable @count
to define the row limit.
The above is the detailed content of How to Correctly Use Dynamic TOP Clauses in SQL Server?. For more information, please follow other related articles on the PHP Chinese website!