sql server - T-SQL Cursor never ends -
i searched google didn't find answer question. in internet says correct. problem - trying write values saved cursor (yes, that). loop use infinity , writes values cursor start end, after again , again , again ...
create procedure createoreditclient(...parameters...) declare c cursor select column table; declare @wrt varchar(20); declare @tmp int = 0; begin open c; fetch next c @wrt; while @@fetch_status = 0 begin print cast(@tmp varchar(10)) + ' ' + @wrt; set @tmp = @tmp +1; fetch next c @wrt; end; close c; deallocate c; end; exec createoreditclient ...;
in opinion cursor written correct output is:
0 790710/1112 1 900519/5555 2 790716/7877 .... 19 111111/1111 0 790710/1112 1 900519/5555 ....
and in end writes error message "maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32)."
if use
select column table;
it writes 20 records.
i know there better solution example need know why cursor doesn't work. may useful in future. every asnwer.
ok, in comments under question solution.
"exec procedurename ...;" can't in same file procedure's body when compile.
create procedure createoreditclient(...parameters...) declare c cursor select column table; declare @wrt varchar(20); declare @tmp int = 0; begin open c; fetch next c @wrt; while @@fetch_status = 0 begin print cast(@tmp varchar(10)) + ' ' + @wrt; set @tmp = @tmp +1; fetch next c @wrt; end; close c; deallocate c; end;
the example in question call procedure recursively. all.
Comments
Post a Comment