2

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;
1
  • When you use TOP you also need to specify ORDER BY. Otherwise you have no way to ensure which rows will be returned. Commented Nov 30, 2016 at 14:37

3 Answers 3

3

You were almost there.

declare @rows int=2;
select top (@rows) * from table_name;
Sign up to request clarification or add additional context in comments.

Comments

0

You can also try this one.

declare @rows int=2;
declare @query varchar(200)= 'select top ' + cast(@rows as varchar(4))+ ' * from TBLB_Order';
exec (@query);

Comments

0
declare @rows int=2;
select top @rows percent * from table_name;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.