Geeks With Blogs
Bunch's Blog One day I'll have a catchy subtitle, one day

I came across this the other day. Using SQL 2000 I could not get a Top statement to work with a variable. I had forgotten that only with SQL 2005 and higher could you do this trick. I found a good way around it, you can read about it here. Anyway this works on SQL 2000:

declare @top int
set @top = 5

set rowcount @top
 select Some_ID
 from tblStuff
set rowcount 0

The code above would return the first five records from tblStuff. For any of the newer SQL Server version you can simply use the @top variable in Top like:

select top (@top) Some_ID
from tblStuff


Posted on Tuesday, May 15, 2012 1:48 PM | Back to top

Comments on this post: Old SQL, Top and a Variable

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Bunch | Powered by: