Bunch's Blog

One day I'll have a catchy subtitle, one day
posts - 87 , comments - 122 , trackbacks - 0

Tuesday, May 15, 2012

Old SQL, Top and a Variable

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

BEGIN
set rowcount @top
 select Some_ID
 from tblStuff
set rowcount 0
END

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

Tags:

Posted On Tuesday, May 15, 2012 1:48 PM | Comments (0) |

Powered by: