sql server - Database cursor + while loop within -
this function have created return recorded stock numbers in table "contributordeeds" table contains start number of stocks , end number of stocks (e.g, number - number) need numbers between within range.
the function works fine, however, tasks several minutes return result :(.
is there better sql query same result?
function [dbo].[stocknumbers] () returns @sequencenumbersofshares table (stocknumber nvarchar(50)) begin declare @deeddocumentid bigint declare @min nvarchar(50) declare @max nvarchar(50) --create table #localtemptable(stocknumber nvarchar(50)) declare db_cursor cursor select contributordeedsid dbo.contributordeeds deedfromshare not null , deedtoshare not null open db_cursor fetch next db_cursor @deeddocumentid while @@fetch_status = 0 begin set @min = (select deedfromshare contributordeeds contributordeedsid = @deeddocumentid) set @max = (select deedtoshare contributordeeds contributordeedsid = @deeddocumentid) while dbo.insertsp(@min, 20) <= dbo.insertsp(@max, 20) begin insert @sequencenumbersofshares(stocknumber) values(@min) set @min = @min + 1 end fetch next db_cursor @deeddocumentid end close db_cursor deallocate db_cursor return; end;
Comments
Post a Comment