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)
 Parameterized TOP N

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-05-16 : 09:46:03
Willeus writes "I'd like to return the top N records of a table with N being passed as a parameter to the stored proc, but when I tried the following code, it was flagged as an error:


SELECT TOP @N * FROM MyTable


Is there another way to do this in Microsoft SQL Server 2000, besides resorting to creating a string and doing an EXEC or sp_executesql call, or using SET ROWCOUNT?

I could, of course, use a temporary table with an autoincrement column, insert all the rows from MyTable into that temp table, then compare that column versus @N, but that's way too intensive especially on a table with 50,000+ records.

Any other way of doing it?"

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-05-16 : 09:52:56
Try

set rowcount @N
select * from ....
set rowcount 0



- Jeff
Go to Top of Page

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2003-05-16 : 12:03:31
Just a note :
A quick search on SQLTeam for TOP
http://www.sqlteam.com/SearchResults.asp?SearchTerms=top

and you would have had your question answered in 10 seconds.

http://www.sqlteam.com/item.asp?ItemID=233

Damian

Edited by - merkin on 05/16/2003 12:03:59
Go to Top of Page
   

- Advertisement -