I am trying to using a SQL data reader to move records from one table into an archive table. I want to do the transfer record-by-record (instead of inserting and deleting en masse) so that if an error occurs, the minimum amount of data is lost (and has to be restored from backup).
My question is, will SQL reader work if I delete records once they are read? This is by far the most simple and direct way to do what I am trying to do. Will it open unexpected errors?
While myReader.Read // select * from tableA where older than 1 year record
insert the record into tableA_archive
delete the record from tableA
loop
This seems vaguely analogous to looping through a list that you are modifying within the loop–which I know causes all sorts of headaches in .net
for aItem in listA
if aItem fits criteria, send to list B and delete from listA
next
Is there any reason not to modify the underlying table while a reader is reading? For instance, does a reader use certain indexes to reach records that might shift if I start deleting records in the middle of the loop?
You should avoid transferring rows all the way down to the client, just to re-upload them back to the server again. Instead, do something like this (within the same transaction):
Or even MS SQL Server specific:
(all conditions are same)
You can even execute these statements from your .NET code if you wish, and still retain the benefit of not downloading all the rows.