title: Inserts into Sybase table in the cursor fetch loop
body: <p>I am writing a Sybase stored procedure with a cursor fetching records from table and inserting records back. Surprisingly I found that fetches see amd return records inserted in the same loop. Under some conditions this results in endless loop, sometimes the loop ends inseting a number of extra records. Looking through Sybase documentation I could not find a cure. Transaction isolation does not help since we are acting inside a single transaction. Of course I could solve the problem by inserting into a temporary table in the loop and then inserting back into main table after the loop ends.
  But the question remain: <strong>how can I isolate cursor fetches from inserts into the same table</strong>?
  Pseudocode follows:</p>
  
  <pre><code>create table t (v int)
  go
  insert into t(v) values (1)
  create procedure p as
  begin
   declare @v int
   declare c cursor for select v from t
   begin transaction
   open c
   while 1=1 begin
    fetch c into @v
    if (@@sqlstatus != 0) break
    insert into t (v) values (@v+1)
   end
   close c
   commit
  end
  go
  exec p
  go
  </code></pre>