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
 SQL Server Development (2000)
 SELECT TOP Query with variable Integer

Author  Topic 

dotnetallday
Starting Member

16 Posts

Posted - 2004-01-16 : 04:35:57
I'd like to perform a SELECT TOP n * FROM Table1 where "n" is equal to a parameter passed. I've got something like this but obviously it doesn't work. I'm somewhat of a newbie to SQL, so please excuse me if the question seems idiotic.

CREATE PROC dbo.GetTopAnswers
(
@Count int
)
AS

SELECT TOP @COUNT * FROM tblAnswers

GO

Thanks!

nr
SQLTeam MVY

12543 Posts

Posted - 2004-01-16 : 04:47:12
set rowcount @COUNT
select * from tblAnswers
set rowcount 0

or
declare @sql varchar(8000)
select @sql = 'select top ' + convert(varchar(20),@COUNT) + ' from tblAnswers'
exec (@sql)


or
wait for about 10 months.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

harshal_in
Aged Yak Warrior

633 Posts

Posted - 2004-01-16 : 04:47:55
create PROC dbo.GetTopAnswers
(
@Count int
)
AS
declare @sql nvarchar(1000)
set @sql='SELECT TOP '+convert(varchar(10),@COUNT)+' * FROM tblanswers'
exec sp_executesql @sql

GO


He is a fool for five minutes who asks , but who does not ask remains a fool for life!
Go to Top of Page
   

- Advertisement -