T-SQL – Using a parameter in SELECT TOP clause

I’ve been working with Stored Procedures a lot recently, even though the whole idea was new to be before starting this job. Today I was trying to select the first n rows from a table, but wanted to be able to change n via a parameter in the procedure call. I thought something like the following would work, but it throws an error when trying to use the variable for the TOP clause.

CREATE PROCEDURE dbo.sp_TestGetAll
    @LIMIT INT
AS
SELECT TOP @LIMIT
    id, col1, col2, col3
FROM TestTable

After a brief Google search there were a few methods presented including such nasties as manually writing out a SQL string within the procedure and executing that instead! Also, it was suggested to change the ROWCOUNT variable before and after issuing the select. Sheesh, that’s messy.

Well anyway, the solution which works for me and is much easier is simply to wrap the variable in parenthesis and magic… it works.

CREATE PROCEDURE dbo.sp_TestGetAll
    @LIMIT INT
AS
-- Notice the parentheses around @Limit - that is the only change!
SELECT TOP (@LIMIT)
    id, col1, col2, col3
FROM TestTable

I’m just really noting this down so that I don’t forget, do another Google search and then resort to those other ugly ways of doing this simple task.

Previous
Next

37 Comments on T-SQL – Using a parameter in SELECT TOP clause.

Add your two pennies, others have already donated theirs.

  1. Sujith
    Sujith
    Apr 9 2007 at 11:27 BST

    Hi

    I think this works only with SQL 2005.
    I tried with SQL 2000, it is not working.

    Thanx
    Sujith

  2. Drew Noakes
    Jul 16 2007 at 13:10 BST

    Thank you for this — saved me a lot of time. This page came up as the first link in Google for my search.

  3. Barry Bartlett
    Barry Bartlett
    Aug 2 2007 at 22:03 BST

    Nice one. I shall use it with a Report I am writing.

  4. Neel Bhatt
    Sep 10 2007 at 10:50 BST

    Hi,

    Thanks a lot. To get around this problem, I was constructing an SQL query in the stored procedure and executing it.

    Now this tip made it really simple.

    Thanks & Regards,
    Neel.

  5. Michael Colp
    Sep 21 2007 at 22:18 BST

    Thanks, you just saved me what would have surely been a huge headache.

  6. Tom Lea
    Nov 9 2007 at 15:28 BST

    For SQL 2000 try:

    SET ROWCOUNT @PageSize;
    SELECT a,b,c,d from e where f=’g’
    SET ROWCOUNT 0

    I’m sure this is available elsewhere, but this is the most promising google result!

  7. Ofer Presente
    Ofer Presente
    Jan 13 2008 at 14:23 BST

    The parameterized top does work (only in 2005 as was stated here), but it can be very slow sometimes. I wrote a query that uses

    set @Limit=40
    select top (@Limit) …..

    This query takes 15 seconds. When the very same query is changed to
    select top (40) ….

    it takes 3 seconds – 1/5 of the duration of the parameterized top!!!

  8. John
    John
    Apr 2 2008 at 08:08 BST

    This had me really excited, until I tried it. I concur with what the last poster wrote. I used this trick to query a table in SQL Server 2005 and my query went from less than a second with a hardcoded value for TOP, to 26 seconds (!!) using the same value in a parameterized TOP. Boo!

  9. Andell
    Andell
    Apr 27 2008 at 12:47 BST

    SELECT Top (@Variable) … , actially works on SQL2005

  10. CLINKS
    CLINKS
    Jan 20 2009 at 23:50 BST

    Thanks! Save me a lot of time as well!

  11. Vladimir
    Vladimir
    May 6 2009 at 09:40 BST

    Its what i have had problems with as well. Quick & clear solution! Thanks!

  12. EK
    EK
    Jul 16 2009 at 10:49 BST

    Thanks for writing this had a series of if statements otherwise, ta

  13. Dean
    Dean
    Jul 28 2009 at 20:36 BST

    Thanks for the tip, it works great. I’m using SQL Server 2008 and I am not noticing any performance issues.

  14. Petulka77
    Petulka77
    Jan 6 2010 at 01:15 BST

    Thnks! That is what I am looking for!

  15. BruceS
    BruceS
    Feb 15 2010 at 05:24 BST

    Many thanks. I have been looking around for this answer.

  16. Dao T
    Dao T
    Jul 28 2010 at 07:05 BST

    Thanks alot, That great tip saved me lots of time !!!

  17. michael
    Oct 12 2010 at 19:43 BST

    thanks for code, it works in sql 2005 express…

  18. tamer
    tamer
    Oct 20 2010 at 18:01 BST

    3.5 years after your post, and it’s still helpful, saved me from the nasty construction of a string and executing it….much cleaner here!!!

  19. Gripper
    Gripper
    Oct 22 2010 at 13:34 BST

    Genius! That saved me hours of messing about trying to use EXEC etc….

    Many thanks!

  20. Roy
    Roy
    Nov 10 2010 at 01:29 BST

    Tks, it’s great!

  21. Afshar Mohebbi
    Nov 26 2010 at 10:50 BST

    Many thanks! helped me a lot!

  22. Dan
    Dan
    Nov 29 2010 at 23:44 BST

    How about selecting like this? I cant figure out the right formatting to make it work.

    SELECT @TableName.@ColumnName
    FROM @TableName

  23. Michael
    Michael
    Dec 23 2010 at 21:18 BST

    Thanks!

  24. Praveen
    Jan 15 2011 at 05:05 BST

    Good one. This is the way we have to use.
    http://praveenbattula.blogspot.com/2011/01/using-variables-in-top-clause-in-t-sql.html

  25. KB
    KB
    Mar 4 2011 at 05:03 BST

    Thanks ! First search result on google and very helpful.

  26. CMH
    CMH
    May 4 2011 at 21:56 BST

    Thank You! saved me from nasty dynamic SQL.

  27. Anthony
    Anthony
    Jul 19 2011 at 01:59 BST

    Thanks. I was going to dynamic SQL.

  28. Lazar Toronto
    Lazar Toronto
    Aug 12 2011 at 17:39 BST

    U r KING!! It helped a lot!! :)

  29. Peter Mankge
    Peter Mankge
    Oct 3 2011 at 09:48 BST

    Thanks very much, my last option was to break my long sql stament and try different approach, this helped me to save a lot of time, thanks once again, you are a star.

  30. Gregor
    Gregor
    Nov 16 2011 at 08:05 BST

    You are great! Tanks very much!!

  31. Randall Sexton
    Randall Sexton
    Mar 1 2012 at 05:33 BST

    Dude, you rock! Thanks!

  32. Niall
    Mar 21 2012 at 19:53 BST

    :-) Solid gold! I love the simple ones !

  33. Dave
    Sep 10 2012 at 09:02 BST

    Awesome, just helped me out ;-)

  34. Paul Chernoch
    Paul Chernoch
    Nov 9 2012 at 15:58 BST

    I didn’t know this was possible. Thanks a million for saving me from dynamic SQL.

  35. mAHESH
    mAHESH
    Dec 7 2012 at 08:40 BST

    saved my time

  36. Jimme
    Jimme
    Dec 7 2012 at 21:49 BST

    SET ROWCOUNT @PageSize works thanks

  37. Don Blaire
    Don Blaire
    Jan 17 2013 at 19:27 BST

    I was pulling my hair out trying to figure this out. Your solution is just what I was looking for. Thanks.

Post Comment