/ Published in: SQL
Basic efficient (!) cursor for looping a set. LOCAL is scope, FASTFORWARD means fetch next is the only allowed command, and it also implies readonly.
Expand |
Embed | Plain Text
SET nocount ON declare @asset_id varchar(40) declare @suffix varchar(4) declare @count int SET @count = 0 declare iterate cursor LOCAL FAST_FORWARD FOR SELECT DISTINCT asset_id, suffix FROM tbl_src open iterate fetch next FROM iterate INTO @asset_id, @suffix --loop all items while @@fetch_status = 0 begin --do something with current item INSERT INTO tbl_asset (asset_id, suffix, can_have_multiple) VALUES (@asset_id, @suffix, 0) IF @@ROWCOUNT > 0 begin SET @count = @count + 1 end --grab next item fetch next FROM iterate INTO @asset_id, @suffix end close iterate deallocate iterate print '' print '(' + convert(varchar,@count) + ' row(s) affected)' print '' SET nocount off
You need to login to post a comment.
