Question

We have a few tables with over 300,000 records and it takes several hours for records to be transferred/copied from one table to another table. Is there a more efficient effective way than using cursors and copying each record one by one from one table to another table, when dealing with large quantities of data?

Code:

open SOMETBL 
fetch SOMETBL into @key1,@key2,@key3,@key4
while(@@fetch_status = 0)
 begin

  SELECT @key1InMapping = count(*) FROM SOMEOTHERDB.dbo.tblSOMETBLping WHERE fldEServicesKey = @key1

  SELECT @eServiceTypeKey = fldAServiceTypeKey FROM SOMEOTHERDB.dbo.tblAServiceType WHERE fldAServiceTypeNumber = @key4

 if (@eServiceTypeKey=null or @eServiceTypeKey=0)
           set @eServiceTypeKey = 50

  if @key1InMapping>0
  begin
    update SOMEOTHERDB.dbo.tblSOMETBLping set fldAServiceTypeKey=@eServiceTypeKey where fldEServicesKey= @key1
   -- print 'post='+convert(varchar,@key2) + ' :key1='+convert(varchar,@key1)+ ' :serviceTypeKey='+convert(varchar,@eServiceTypeKey)+' : serviceTypeNum='+convert(varchar,@key4)
  end
  fetch SOMETBL into @key1,@key2,@key3,@key4 

end

close SOMETBL 
Was it helpful?

Solution

300,000 records is a small amount of data in database terms. But it is too large to be using cursors which really should not be used for more than a couple of hundred records and frankly once you learn to write set-based code, it is generally shorter and takes less time to write than a cursor anyway. So I would never use a cursor as a first choice, it is a technique of last resort. You should not be thinking about inserting/updating or deleting one record at a time in a cursor. You use set-based operations. Now with 300,000 records you might want to consider a combination of set-based and cursoring where you process a group of recorss at a time (say 10,000) rather than cursor one a time.

Checke out the following for details on how to change a cursor to set-based code. http://wiki.lessthandot.com/index.php/Cursors_and_How_to_Avoid_Them

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top