Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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.4k points)

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

SELECT TOP (@count) * 

FROM someTable

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...