title: Using SQL cursors instead of self-join to find non - consecutive numbers
body: <p>In SQL Server, I would like to check if numbers in a specific column are following each other in ascending order using SQL Cursor Functions. Basically each row must have a greater value than the previous one. I have written a code using <strong>self-join</strong> as shown below:</p>
  
  <p>First I have given each row a row number that is defined as integer, in order to compare each row with the previous one using <code>select row_number() over(ORDER BY (SELECT NULL) ) AS Row#</code>. Then compared the table with itself using the row number created above and found the rows that are not consecutive using the code below.</p>
  
  <pre><code>  SELECT *
    FROM Sequence AS prev_row
    JOIN Sequence AS next_row 
    ON prev_row.Row#+1 = next_row.Row#
    AND (prev_row.Number &gt; next_row.Number)
  </code></pre>
  
  <p>Even though my code above is working fine, I wanted to perform the same operation using SQL Cursors to have a better understanding of usage of cursors and develop a different approach on SQL methods. So I wrote the following code, but it is not working:</p>
  
  <pre><code>DECLARE db_cursor CURSOR 
  FOR
  SELECT Row#, Number
  FROM Sequence;
  
  DECLARE @Row# int;
  DECLARE @Numbers int;
  
  OPEN db_cursor
  FETCH NEXT FROM db_cursor
  
  WHILE @@FETCH_STATUS = 0
  BEGIN
      SELECT @Row# , @Numbers
      FETCH NEXT FROM db_cursor
      SELECT @Row#, @Numbers --this part is for printing results
      where @Row#&gt;@Row#+1 --condition
  END
  CLOSE db_cursor;
  DEALLOCATE db_cursor; 
  </code></pre>
  
  <p>Can someone explain where I go wrong? Help will be appreciated.</p>