Top @n using var in the n quantity

  • I am trying to do an sp that send the top n transaction that the user want, but I don't know how to do it, this isn't works:

    declare @n as int

    set @n=3

    SELECT TOP @n notrans, NOTARJETA, NOCLIENTE

    FROM TBMOVIMIENTOS

    WHERE NOCLIENTE=10 AND NOEMPLE='0000000124'

    ORDER BY nocliente, noemple, NOTRANS desc

  • Place parentheses around @n:

    SELECT TOP (@n) notrans, NOTARJETA, NOCLIENTE

     

    -Eddie

    Eddie Wuerch
    MCM: SQL

  • I do the next change, and I get and error:

    Server: Msg 170, Level 15, State 1, Line 5

    Line 5: Incorrect syntax near '('.

    What I am doing wrong?

    declare @n as int

    set @n=3

    select @n

    SELECT TOP (@n) notrans, NOTARJETA, NOCLIENTE

    FROM TBMOVIMIENTOS

    WHERE NOCLIENTE=10 AND NOEMPLE='0000000124'

    ORDER BY nocliente, noemple, NOTRANS desc

  • that works for 2005

    you must be using 2000

     

  • declare @n as int

    declare @sql_cmd as varchar(4000)

    set @n=3

    set @sql_cmd='SELECT TOP '+cast(@n as varchar(10))+' notrans, NOTARJETA, NOCLIENTE

    FROM TBMOVIMIENTOS

    WHERE NOCLIENTE=10 AND NOEMPLE='+char(39)+'0000000124'+char(39)+'

    ORDER BY nocliente, noemple, NOTRANS desc'

    exec (@sql_cmd)

     

     

    should work for 2000

  • Yes I am using 2000, thank you for you help.

Viewing 6 posts - 1 through 5 (of 5 total)

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