passing variable to TOP option

  • Here is the command that I need to run

    INSERT INTO #B SELECT TOP @TOPCNT FROM #A

    My first problem is that I can't use a variable as the parameter in the TOP command.

    I have tried putting the SQL into a variable and then using the EXEC (@command), but the EXEC can't reference local temporary tables.

    Any suggestions?


    Live to Throw
    Throw to Live
    Will Summers

  • Use 'set rowcount' in sql 2000...

    http://www.aspfaq.com/show.asp?id=2521

    Ryan Randall

    Solutions are easy. Understanding the problem, now, that's the hard part.

  • You are correct, you cannot use a variable with TOP.  Try this instead:

    DECLARE @TopCnt int

    SET @TopCnt = 100

    SET ROWCOUNT @TopCnt

    INSERT INTO #B

    SELECT *

    FROM #A

    SET ROWCOUNT 0 --Make sure you set the RowCount back to zero!!

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • That worked thanks.


    Live to Throw
    Throw to Live
    Will Summers

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply