sql server - T-SQL Stored Procedure with Cursor Loop causing Errors -


could please explain me why have error message show when exec stored procedure. "a cursor name 'tname_cursor' exists"

declare @tname varchar(100) declare @lsql varchar(8000)    declare tname_cursor cursor                      select name sys.tables type = 'u' , name 'ppm_metric%' open tname_cursor; fetch next tname_cursor @tname;  while @@fetch_status = 0 begin     set @lsql = n'         update [' +@tname+ ']            set latestofall_flg = ''n''           [' +@tname+ '] t            join d_custom_metrics_rule  s             on t.metric_id = s.custom_metric_rule_id            , t.latestofall_flg = ''y'''      execute sp_executesql @lsql     fetch next tname_cursor @tname;    end close tname_cursor; deallocate tname_cursor;  declare tname_cursor_redo cursor                 select name sys.tables type = 'u' , name 'ppm_metric%' open tname_cursor_redo; fetch next tname_cursor_redo @tname;  while @@fetch_status = 0 begin     set @lsql = n'                   update [' +@tname+ ']                set latestofday_flg = ''n''               [' +@tname+ '] t                join d_custom_metrics_rule  s                 on t.metric_id = s.custom_metric_rule_id                , t.calc_metric_date_id = convert(int,convert(varchar, getdate(), 112))                , t.latestofday_flg = ''y'''      execute sp_executesql @lsql     fetch next tname_cursor_redo @tname;   end close tname_cursor_redo; deallocate tname_cursor_redo; 

it sounds may using global cursors (?). if don't need them, suggest using local cursors. e.g.

declare tname_cursor_redo cursor local                 select name sys.tables type = 'u' , name 'ppm_metric%' 

that may make things more robust.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -