Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Stored procedure parameter for top X results

Author  Topic 

adlo
Posting Yak Master

108 Posts

Posted - 2004-08-31 : 13:15:07
I want to pass a parameter
@num to a stored procedure to return the top @num results back

Select top @num .....
from Table

It doesn't allow it and says incorrect syntax.

Is this possible?

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-08-31 : 13:18:38
two ways to do it:

declare @v1 int
set @v1 = 25
set rowcount @v1
select * from MyTable Order by DateColumn
set rowcount 0

declare @vSQL varchar(1000), @numrows int
select @numrows = 25
select @vSQL = 'select top ' + convert(varchar, @numrows) + ' * from MyTable Order by DateColumn'
Execute (@vSQL)

Go with the flow & have fun! Else fight the flow :)
Go to Top of Page

adlo
Posting Yak Master

108 Posts

Posted - 2004-08-31 : 13:24:04
Thanks
Go to Top of Page
   

- Advertisement -