You are here:Home » database » How to use variable with TOP clause in SQL statement without using dynamic query in SQL Server

How to use variable with TOP clause in SQL statement without using dynamic query in SQL Server

-- Here is the example
--#1
Declare @top int
set @top = 10 -- set @top to 10 to get top 10 record from restult
select top (@top) * from TableName

--#2
Declare @top int
set @top = 10 -- set @top to 10 to get top 10 record from restult
set rowcount @top --set rowcount to 10
select * from TableName
set rowcount 0 --- Reset rowcount to 0