Declare TOP

  • Hello guys,

    Is there a way to declare TOP? something like this:

    DECLARE @Rows int

    SET @Rows = 100

    select top @Rows Fname, Lname, Addr1, Addr2, City, State, Zip, phone from tblname

    Thanks

  • SET ROWCOUNT

    _____________
    Code for TallyGenerator

  • thanks...

  • SET ROWCOUNT can sometimes be a lot slower than TOP... if it turns out to be too slow, you could always use dynamic SQL (observing, of course, all the warnings about dynamic SQL that may be exposed to the outside world, etc).

    DECLARE @Rows VARCHAR(10)

    SET @Rows = 100

    DECLARE @sql VARCHAR(8000)

    SET @sql = 'SELECT TOP ' + @Rows + ' Fname, Lname, Addr1, Addr2, City, State, Zip, phone FROM tblname'

    EXEC (@SQL)

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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