Hi Below cursor logic takes 2 hrs to run can you please let me know suggest alternate logic to replace while loop.
declare
@col1
@col2
@col3
@colsum
@colsum1
@col4
@col5
@col6
Declare Cursor1 cursor for
select col1,col2, col3, colsum = (select count(*) from table2 where table2.col1 = table1.col1) from table1
open Cursor1
Fetch next from Cursor1 into
@col1
@col2
@col3
@colsum
while (@@ fetch_status = 0)
Begin
SET @colsum1 = @col2 + @col3
if @colsum = 0
Begin
Insert into Table2 (col1, col2, col3 ,col4,Ind)
values
(@Col1,@col1+@col2,@col3,@colsum1,1)
End
Else
Begin
select @col4 = col1,
@col5 = col2
from table2
where col3 = @ col3 and col4 = @colsum1 and Ind = 1
set @col1 = @col4 + col1
set @col2 = @col5 + col2
update table2 set Ind = 0
where col3 = @ col3 and col4 = @colsum1 and Ind = 1
Insert into tabl2 (col1, col2, col3 ,col4,Ind)
values
(@Col1,@col1+@col2,@col3,@colsum1,1)
End
Fetch next from Cursor1 into
@col1
@col2
@col3
@colsum
End
Close Cursor1
Deallocate cursor1