variable use in top keyword

  • is there any way to use the variable with top keyword?

    for eg:

    declare @val as int

    set @val=5

    select top @val * from storemaster

  • You can do exactly that in SQL 2005. In SQL 2000 you had to do dirty hacks like using SET ROWCOUNT @val and then, after your select statement, set ROWCOUNT 0 to remove the rowcount restriction.

    However, you have a slight syntax problem. You need brackets around @val, so it looks like

    select top(@val) * from mytable

Viewing 2 posts - 1 through 1 (of 1 total)

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