Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language 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 Search this Thread Display Modes
  #1 (permalink)  
Old December 23rd, 2004, 09:44 PM
Registered User
 
Join Date: Dec 2004
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Select row number N

Hi all,
How do we select the n'th row from a table?
Lets say I want to pick the 7th row, how should i do it? I already have an IDENTITY column, but I can't select based on it since the number might be skipped by delete statements..

I'm using Ms SQL 2000..

Reply With Quote
  #2 (permalink)  
Old December 23rd, 2004, 10:21 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: Lehigh Acres, FL, USA.
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

you can this something like this

Code:
SELECT TOP 7 *
FROM
SELECT TOP 500 *
FROM myTable
ORDER BY id) AS TopOfTable
ORDER BY id DESC
Jaime E. Maccou
Reply With Quote
  #3 (permalink)  
Old December 26th, 2004, 09:20 PM
Registered User
 
Join Date: Dec 2004
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply jemacc..

I've change the SQL to this:
Code:
SELECT TOP 1 *
FROM
(SELECT TOP 7 *
FROM myTable
ORDER BY id) AS TopOfTable
ORDER BY id DESC
but i want the number 'N' to be a variable, but this code wont work:
Code:
DECLARE @wanted int
SET @wanted = 7

SELECT TOP 1 *
FROM
(SELECT TOP @wanted *
FROM myTable
ORDER BY id) AS TopOfTable
ORDER BY id DESC
It says "Incorrect syntax near '@wanted'". The number for TOP can't be a variable?


Reply With Quote
  #4 (permalink)  
Old December 27th, 2004, 01:02 AM
Friend of Wrox
 
Join Date: Nov 2003
Location: Lehigh Acres, FL, USA.
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

Code:
CREATE PROCEDURE TOP_TEST
(@topn char(6))  
AS 
declare @sqlstring  nvarchar(2000) 
declare @parmstring  nvarchar(2000) 

set @sqlstring='select top '    
    +@topn
    +' * from MYTABLE' 
set @parmstring='@topn varchar(20)' 
exec sp_executesql @sqlstring,@parmstring,' @topn = @topn' 

exec test 7
Jaime E. Maccou
Reply With Quote
  #5 (permalink)  
Old December 27th, 2004, 06:22 AM
Registered User
 
Join Date: Dec 2004
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

wow, first time seeing exec sp_executesql..
thanks jemacc

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 row in GridView WITHOUT using Select button rao965 ASP.NET 2.0 Professional 1 February 15th, 2008 10:44 AM
To retrieve a row by giving row number prad_a MySQL 1 March 22nd, 2007 11:20 PM
Serial Number with each table row ketannsharma Access 3 March 16th, 2007 06:44 AM
help in xsl getting row number pandian XSLT 6 January 23rd, 2006 10:00 AM
How to select every N row in CR nan Crystal Reports 4 September 3rd, 2004 07:32 AM



All times are GMT -4. The time now is 08:15 PM.


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