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

declare @top  int

set @top = 5

select top @top * from tablename

Is it possible?

Or any idea for such a logic (i don't want to use dynamic query)?

1 Answer

0 votes
by (40.7k points)

Yes, it is possible to use the variable with TOP in select statement in SQL Server without making it dynamic. 

In SQL Server 2005, you can use a variable in the top clause in this way:

select top (@top) * from tablename

Browse Categories