Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2013

    Unanswered: How to Fetch exactly the i-th row of a table in Sybase

    I need to fetch exactly the i-th row of a sybase table.

    To elaborate - the below query returns 3 rows.

    select * from tab1 where var1 = 1

    Now I want to fetch exactly the second row that is returned. I am trying the below query -

    select top 1 start at 2 * from tab1 where var1 = 1 order by row_id

    But it is not working in Sybase Advantage. Can anyone suggest any alternative method?

    I do not intend to use a cursor.

  2. #2
    Join Date
    Jan 2004
    Provided Answers: 4
    Why do you want to retrieve the n-th record? What is the significance of exactly thát record? There is no reason why the n-th record is the n-th. It depends on what page it is stored in, indexes, query plans and whatnot.
    I'm not crazy, I'm an aeroplane!

  3. #3
    Join Date
    Dec 2013
    Hi Martijnvs,

    Actually I could ease up things by use of nested cursor. But since we can not put nested cursor in the same block of code, and given the fact I do not have access to Create a Procedure, I had to find a workaround.

    The requirement is somewhat like below -
    I am fetching the set of rows returned by the below query and putting it in a cursor -

    select col1, col2, col3 from tab1 where col1 = 123.

    Now depending on the value of col1, I have to query tab2 or tab3.
    like - if col1 = 1 query tab2 as - select * from tab2 where col2 = col2 value from tab1.
    else query tab3.

    Now select * from tab2 where col2 = col2 value from tab1 can return multiple rows, and all of them need to be displayed.
    So I am I need a row pointer (which can not be a cursor) anf traverse the outcome. Hence I need to get a way of fetching the i-th row with order by clause on col2.

  4. #4
    Join Date
    May 2005
    South Africa
    Provided Answers: 1
    This seem related to your other post see

  5. #5
    Join Date
    Feb 2012
    create table #Employee (Employee_ID int null, Salary float null)

    insert into #Employee values(100,5000)
    insert into #Employee values(200,10000)
    insert into #Employee values(300,15000)
    insert into #Employee values(400,7000)
    insert into #Employee values(500,5000)
    insert into #Employee values(600,2000)
    insert into #Employee values(700,3000)

    select * from #Employee

    SELECT *
    FROM #Employee Emp1
    WHERE (N-1) = (
    FROM #Employee Emp2
    WHERE Emp2.Salary > Emp1.Salary)

    drop table #Employee

    --N= i th row of table

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts