I have declared a temp table to hold all the required values as follows:
DECLARE @temp TABLE
(
Password INT,
IdTran INT,
Kind VARCHAR(16)
something like this?
DECLARE maxval, val, @ind INT;
SELECT MAX(ID) as maxval FROM table;
while (ind <= maxval ) DO
select `value` as val from `table` where `ID`=ind;
CALL fn(val);
SET ind = ind+1;
end while;
you could use a cursor:
DECLARE @id int
DECLARE @pass varchar(100)
DECLARE cur CURSOR FOR SELECT Id, Password FROM @temp
OPEN cur
FETCH NEXT FROM cur INTO @id, @pass
WHILE @@FETCH_STATUS = 0 BEGIN
EXEC mysp @id, @pass ... -- call your sp here
FETCH NEXT FROM cur INTO @id, @pass
END
CLOSE cur
DEALLOCATE cur
You can do something like this
Declare @min int=0, @max int =0 --Initialize variable here which will be use in loop
Declare @Recordid int,@TO nvarchar(30),@Subject nvarchar(250),@Body nvarchar(max) --Initialize variable here which are useful for your
select ROW_NUMBER() OVER(ORDER BY [Recordid] ) AS Rownumber, Recordid, [To], [Subject], [Body], [Flag]
into #temp_Mail_Mstr FROM Mail_Mstr where Flag='1' --select your condition with row number & get into a temp table
set @min = (select MIN(Rownumber) from #temp_Mail_Mstr); --Get minimum row number from temp table
set @max = (select Max(Rownumber) from #temp_Mail_Mstr); --Get maximum row number from temp table
while(@min <= @max)
BEGIN
select @Recordid=Recordid, @To=[To], @Subject=[Subject], @Body=Body from #temp_Mail_Mstr where Rownumber=@min
-- You can use your variables (like @Recordid,@To,@Subject,@Body) here
-- Do your work here
set @min=@min+1 --Increment of current row number
END
Try returning the dataset from your stored procedure to your datatable in C# or VB.Net. Then the large amount of data in your datatable can be copied to your destination table using a Bulk Copy. I have used BulkCopy for loading large datatables with thousands of rows, into Sql tables with great success in terms of performance.
You may want to experiment with BulkCopy in your C# or VB.Net code.
You always don't need a cursor for this. You can do it with a while loop. You should avoid cursors whenever possible. While loop is faster than cursors.