How to Get nth row from table

  • Hi 

    How i get n th row from table in ms-sql server

    needed sample programme



  • Hey,

    Check if this SQL is what you are looking for:

    -- example: getting the 1000th row

    declare @N_row as bigint

    set @N_row = 1000 

    -- option to tell SQL Server to only return @N_row rows

    set rowcount @N_row

    -- querying for the @N_row in a table

    select

     s.*

    from

     someTable s

    where

     rowcount_big() = @N_row

    -- turning off option

    set rowcount 0

    Hope this helps ...

    JP

  • --**************************************

    --

    -- Name: Retrieve rows based on a range

    -- Description:How to retrieve rows base

    -- d on a range. This solution doesn't use

    -- temporary tables or cursors.

    -- By: Umachandar

    --

    --This code is copyrighted and has-- limited warranties.Please see http://

    -- http://www.Planet-Source-Code.com/vb/scripts/Sh

    -- owCode.asp?txtCodeId=160&lngWId=5--for details.--**************************************

    --

    -- Doesn't handle TIES well, but can be

    -- incorporated in logic

    -- Show the count first / logical record

    -- number for example...

    SELECT (select count(*) FROM pubs..authors a2

    WHERE a2.au_lname <= a1.au_lname) AS cnt, *

    FROM pubs..authors a1

    ORDER BY a1.au_lname

    -- Get the 12the & 14th authors from the

    -- ordered list

    SELECT a1.au_lname

    FROM pubs..authors a1

    WHERE (select count(*) FROM pubs..authors a2

    WHERE a2.au_lname <= a1.au_lname) BETWEEN 12 AND 14

    ORDER BY a1.au_lname

    I don't know if this is what you are looking for or not.

    Wes

  • This example gets the eigth row from sysobjects you should be able to adapt it to any table.

    select top 1 * from (select top 8 * from sysobjects order by name) x order by name desc

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply