I have a cursor on a temp table #temp_stage2 and I declare this cursor initially and then I insert values into this temp table. Once the table has been inserted, I open the cursor on the temp table and try to loop through the records in the temp table inorder to log the stage id's which are in the temp table, but the cursor seems to go into an infinite loop after the first value.

Is this because of temp tables losing their scope when we move from one transaction to another?

if (@var3 = 'N' )
exec('insert into #temp_stage2(i_stage) select i_stage from '+@vartbl+' where '+@var1+' is null and feed_cd like ''EAGLE_IDX%''')
exec('update '+@vartbl+' set excl_rsn_cd = ''NOT NULLABLE'' from '+@vartbl+' tbl, #temp_stage2 tmp2 where tbl.i_stage=tmp2.i_stage')

open testcursor3
fetch testcursor3 into @v_fld
while (@@sqlstatus=0)
--print 'In'
select @v_fld_char= convert(char(10),@v_fld)
select @insertsql = @var1
select @insertsql = @insertsql+' for stage id '+@v_fld_char+' for table '+@vartbl
select @insertsql = @insertsql+' table cannot be null'
print @insertsql
--exec('insert into riv_intfc_log values(''EAGLE_IDX'',''FEED'','+@insertsql+',''STA GE_VALIDATION'',''POSTPROC_DB'',null,''dbo'',getda te())')
--fetch testcursor3 into @v_fld
--truncate table #temp_stage2
--print 'Out'
close testcursor3
truncate table #temp_stage2
fetch testcursor2 into @var1,@var2,@var3,@var4