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
 
Old December 27th, 2007, 03:55 AM
Registered User
 
Join Date: Dec 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Rownum in SQLServer

Hi,

I need to create a query that gets the 2 largest number from a table. In orcale the script would have been:

select * from (select number,name from xx order by n desc) where rownum<3.

How do I use rownum in SQL Server (which is not SQL Server 2005)?

Thanks

 
Old December 27th, 2007, 06:00 AM
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

You don't appear to need rownum, just select the TOP 2 rows and order by number descending. Look in Books On Line for the TOP keyword for examples.

--

Joe (Microsoft MVP - XML)
 
Old December 27th, 2007, 07:52 AM
Friend of Wrox
Points: 1,536, Level: 15
Points: 1,536, Level: 15 Points: 1,536, Level: 15 Points: 1,536, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2006
Location: , MI, USA.
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Like this... SELECT TOP 2 Number, Name FROM xx Order By N DESC

Joe is correct, though... there's more info in Books Online... make the time to look it up so that you understand how it works (and doesn't). TOP is not programmable in 2k... it is in 2k5.

--Jeff Moden




Similar Threads
Thread Thread Starter Forum Replies Last Post
SQLserver 2000 vs SQLserver 2005 Express cJeffreywang BOOK: Beginning ASP.NET 2.0 and Databases 0 April 22nd, 2007 09:52 AM
Rownum is SQL SERVER rubina SQL Server 2000 3 December 2nd, 2006 10:57 AM
In rownum subhendude SQL Language 0 December 25th, 2005 02:27 AM
rownum or something like it wanderer SQL Server 2000 1 August 5th, 2003 11:41 AM
sql server 2000 rownum mateenmohd Classic ASP Databases 1 June 9th, 2003 03:14 PM





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