Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 12th, 2006, 01:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Naperville, IL, USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default SELECT TOP n

Does anyone know a way to parameterize a SELECT TOP n .... query so that the n can be passed into the statement as a variable?

I would like to be able to do something like this:

Code:
DECLARE @TopCount integer
SET @TopCount = 100

SELECT TOP @TopCount * FROM MyTable ORDER BY RecordDate
Rand
__________________
Rand
  #2 (permalink)  
Old May 12th, 2006, 01:51 PM
Authorized User
 
Join Date: Sep 2005
Location: , , .
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That will work in SQL server 2005 but not in 2000
you can use SET ROWCOUNT in 2000

DECLARE @TopCount integer
SET ROWCOUNT 100

SELECT * FROM MyTable ORDER BY RecordDate

SET ROWCOUNT 0 --make sure you do this at the end


------------------------------------------ http://sqlservercode.blogspot.com/
  #3 (permalink)  
Old May 12th, 2006, 02:14 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Naperville, IL, USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you SQLMenace for the advice about ROWCOUNT.

It works, but unfortunately it also messes up any subqueries. I guess I'll just have to wait for SQL Server 2005 to be installed here.

Code:
DECLARE @TopCount integer
SET @TopCount = 100

SET ROWCOUNT @TopCount

SELECT * FROM MyTable ORDER BY RecordDate

SET ROWCOUNT 0
Rand
  #4 (permalink)  
Old May 12th, 2006, 04:03 PM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Your alternative is to construct the SQL dynamically and execute using exec or sp_executeSql.

--

Joe (Microsoft MVP - XML)


Similar Threads
Thread Thread Starter Forum Replies Last Post
select top problem keyvanjan SQL Server 2000 5 January 7th, 2006 07:11 AM
SELECT TOP n NOT SELECTING TOP n! ibi SQL Language 8 March 30th, 2005 08:08 PM
SELECT TOP FROM HAVING khatfield29 SQL Language 1 August 23rd, 2004 02:41 PM
SELECT TOP !!!! Jane SQL Language 2 October 17th, 2003 11:22 AM
SELECT TOP ciko1973 SQL Language 5 August 8th, 2003 04:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.