Results 1 to 4 of 4

Thread: Variables in select statement

  1. #1
    Join Date
    Oct 2003
    Posts
    2

    Variables in select statement

    Hi is it possible to use a variable when select the top x many records to be returned e.g.

    select top @recstoreturn from orders

    where @recstoreturn is passed into a stored procedure?

    Cheers
    Mark

  2. #2
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    create proc topx @recstoreturn int as
    declare @query varchar(8000)
    set @query = 'select top ' +convert(varchar,@recstoreturn) +'* from sysobjects'
    exec (@query)

    go

    exec topx 5

  3. #3
    Join Date
    Oct 2003
    Posts
    2
    Thanks, is there any way to do it without creating the text string and using the exec?

  4. #4
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    No. Only options are exec or sp_executesql which also requires you to build a string.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •