I have a table tblLogins which has 200k users data saved in it.
I need to insert 30 records for each user in an another table. I used the cursor for this task. But the script I have written takes a lot of time.
It has inserted data only for 60 thousand users in 2 hours.
I have looked over google for the solution but didn't find anything related to improve the performance.
Below is the script I have written.
DECLARE @LoginID int
DECLARE @DomainID int
DECLARE curDomain CURSOR FAST_FORWARD 
FOR SELECT tbldomains_id FROM  tblDomains
OPEN curDomain 
FETCH NEXT FROM curDomain INTO @DomainID
WHILE @@FETCH_STATUS = 0
BEGIN
 --cur2 starts
 DECLARE curLogin CURSOR FAST_FORWARD 
 FOR SELECT tbllogins_id FROM  tbllogins where tbldomains_id = @DomainID
 OPEN curLogin 
 FETCH NEXT FROM curLogin INTO @LoginID
 WHILE @@FETCH_STATUS = 0
 BEGIN
 --code starts
 if not exists(select 1 from tblWidgetProperties where tblLogin_id = @LoginID)
 begin
 Insert tblWidgetProperties values(1,@LoginID,'isEnabled','True')
 Insert tblWidgetProperties values(2,@LoginID,'isEnabled','True')
 Insert tblWidgetProperties values(3,@LoginID,'isEnabled','True')
 Insert tblWidgetProperties values(4,@LoginID,'isEnabled','True')
 Insert tblWidgetProperties values(5,@LoginID,'isEnabled','True')
 Insert tblWidgetProperties values(6,@LoginID,'isEnabled','True')
 Insert tblWidgetProperties values(7,@LoginID,'isEnabled','True')
 Insert tblWidgetProperties values(8,@LoginID,'isEnabled','True')
 Insert tblWidgetProperties values(9,@LoginID,'isEnabled','True')
 Insert tblWidgetProperties values(10,@LoginID,'isEnabled','True')
 Insert tblWidgetProperties values(11,@LoginID,'isEnabled','True')
 end
 if not exists(select 1 from tblWidgetPosition where tblLogins_id = @LoginID)
 begin
 Insert tblWidgetPosition values(3,1.0,@LoginID)
 Insert tblWidgetPosition values(4,1.01,@LoginID)
 Insert tblWidgetPosition values(5,1.02,@LoginID)
 Insert tblWidgetPosition values(11,1.03,@LoginID)
 Insert tblWidgetPosition values(1,2.00,@LoginID)
 Insert tblWidgetPosition values(7,2.01,@LoginID)
 Insert tblWidgetPosition values(9,2.02,@LoginID)
 Insert tblWidgetPosition values(8,2.03,@LoginID)
 Insert tblWidgetPosition values(6,3.0,@LoginID)
 Insert tblWidgetPosition values(2,3.01,@LoginID)
 Insert tblWidgetPosition values(10,3.02,@LoginID)
 end
 --code ends
 FETCH NEXT FROM curLogin INTO @LoginID
 END
 CLOSE curLogin 
 DEALLOCATE curLogin 
 --cur2 ends
 FETCH NEXT FROM curDomain INTO @DomainID
 END
 
     
     
     
    