Explore Courses Blog Tutorials Interview Questions
0 votes
in SQL by (20.3k points)

How can I have a dynamic variable setting the amount of rows to return in SQL Server? Below is not valid syntax in SQL Server 2005+:

DECLARE @count int

SET @count = 20

SELECT TOP @count * FROM SomeTable

1 Answer

0 votes
by (40.7k points)

Try this code. But, it’s valid only for SQL 2005+.

SELECT TOP (@count) * 

FROM someTable

Browse Categories