Prasanna Kumar J Prasanna Kumar J - 10 days ago 5
SQL Question

how to select top row from a variable using sql server 2008?

Can we select top rows from variable value In sql server 2008?.like this.

declare @rows int=2;
select top @rows * from table_name;
-- instead of select top 2 * from table_name;

Answer

You were almost there.

declare @rows int=2;
select top (@rows) * from table_name;