Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
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 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 23rd, 2004, 09:44 PM
Registered User
 
Join Date: Dec 2004
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..

 
Old December 23rd, 2004, 10:21 PM
Friend of Wrox
 
Join Date: Nov 2003
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
 
Old December 26th, 2004, 09:20 PM
Registered User
 
Join Date: Dec 2004
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?


 
Old December 27th, 2004, 01:02 AM
Friend of Wrox
 
Join Date: Nov 2003
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
 
Old December 27th, 2004, 06:22 AM
Registered User
 
Join Date: Dec 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

wow, first time seeing exec sp_executesql..
thanks jemacc






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





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