Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 12th, 2006, 02: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
Reply With Quote
  #2 (permalink)  
Old May 12th, 2006, 02: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/
Reply With Quote
  #3 (permalink)  
Old May 12th, 2006, 03: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
Reply With Quote
  #4 (permalink)  
Old May 12th, 2006, 05: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)
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 03:41 PM
SELECT TOP !!!! Jane SQL Language 2 October 17th, 2003 12:22 PM
SELECT TOP ciko1973 SQL Language 5 August 8th, 2003 05:35 AM



All times are GMT -4. The time now is 02:14 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.