Results 1 to 2 of 2

Thread: How to compare values of two rows using cursor?

  1. #1
    Join Date
    Mar 2010
    Posts
    5

    How to compare values of two rows using cursor?

    Hello,

    In my following simple code I am trying to compare the values of a column between the current row and the previous row and then out put the results if the values are same. How I can I do that? Also when I print the colum from first row and then from next row, why it is giving me the same result? Thanks.

    Following is my code:

    DECLARE
    @abc float,
    @def datetime,

    @abc2 float,
    @def2 datetime


    DECLARE downTime SCROLL CURSOR FOR
    select D_ABC, D_DEF from tab1

    OPEN downTime
    FETCH First FROM downTime INTO @abc,@def
    FETCH NEXT FROM downTime INTO @abc2,@def2

    print @abc
    print @abc2


    WHILE (@@FETCH_STATUS=0)

    BEGIN

    // Out here I want to print the rowID's of those rows where @abc = @abc2

    END
    CLOSE downTime

  2. #2
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    You need to have next set of FETCH NEXTs inside the while loop. Have you cheched the data in tab1, may be first two rows have same D_ABC values.

Tags for this Thread

Posting Permissions

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