Re: SELECT TOP

From: Pradeep Kutty (pradeepk_at_healthasyst.com)
Date: 01/24/05


Date: Mon, 24 Jan 2005 13:50:48 -0000

Try dynamic sql
heres an eg:-

use Northwind
declare @a int, @str varchar(100)
set @a = 10
set @str = 'select top ' + cast(@a as varchar(100)) + ' * from Orders'
exec (@str)

Prad

"Elie Grouchko" <egrouchko@hotmail.com> wrote in message
news:ORBXQMWAFHA.3988@TK2MSFTNGP11.phx.gbl...
> Hi All
>
> I am writing a stored procedure to retrieve the top n rows of a SELECT
> statement.
>
> I tried passing the number of rows as a variable (topcount in the
> example),
> but I get a syntax error:
>
> Am I missing something?
>
> Thanks in advance
>
> Elie Grouchko
>
> CREATE PROCEDURE Proc_SelectTopForumCommentsOrderByDate
> @topcount int,
> @twc_comment_subject int
> AS
> SELECT TOP @topcount * FROM [twt_comment] WHERE
> ([twc_comment_subject] = @twc_comment_subject)
> AND ([twc_comment_id] != @twc_comment_subject)
> AND ([twc_comment_state] = 2) ORDER BY [twc_comment_date] DESC
>
>



Relevant Pages