Click here to Skip to main content
15,909,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all experts,

i want to display top 10,20 or 8 in sql server 2008 R2.
i declare like this:
declare @top int
set @top=4
set rowcount @top
select top @top ItemCode,ItemName
from item
but it doesn't work and it display an error like this:
"Incorrect syntax near '@top'."

Note:
-variable @top is a dynamic value and based on user inputted.
-I don't want to use rowcount @top coz it make my query slow.

Does anybody know how i can work with top @top in sql server 2008R2?

Thanks

TONY
Posted

 
Share this answer
 
It should be enclosed in ().

try this.

SQL
declare @top int
set @top=4
select top (@top) ItemCode,ItemName
from item
 
Share this answer
 
Comments
ridoy 15-Aug-12 6:08am    
yes
soeun tony 15-Aug-12 6:36am    
Hi Santhosh
It works now

Thanks
Santhosh Kumar Jayaraman 15-Aug-12 6:40am    
Please mark it as solved.So that others will know.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900