Link to home
Start Free TrialLog in
Avatar of Staudte
StaudteFlag for Germany

asked on

MS SQL Server: Modify data while looping through a datareader

Hi fellow experts,

I assume that this is a standard problem, but I haven't found an ideal answer to it.

Simplified, I have a table Persons with columns PersonID and Field1. Right now this table (in real life) contains 50.000 records and is about 200 MB large (with more fields, of course). in VB.net I create two connections, do a SELECT * FROM MyTable WHERE Field1='A' and loop through the connected datareader. In the loop, I do some computations and execute a "UPDATE MyTable SET Field1='B' WHERE PersonID=@PersonID" on the second connection, where @PersonID is fetched from the datareader.

In code, this looks like that:

conn1.Open()
conn2.Open()
cmd1.Connection = conn1
cmd2.Connection = conn2

cmd1.CommandText = "SELECT PersonID, Staat, ...somefields... FROM Personen where Staat IS NULL"
dr = cmd1.ExecuteReader()
Do While dr.Read
  [.. do some calculations ..]
  cmd2.CommandText = "UPDATE Personen SET ...somefields..., Staat=@Staat WHERE PersonID=@PersonID"
  cmd2.Parameters.Clear()
  cmd2.Parameters.Add("@PersonID", Data.SqlDbType.Int).Value = dr("PersonID")
  cmd2.Parameters.Add("@Staat", Data.SqlDbType.Int).Value = 1
  cmd2.ExecuteNonQuery()
Loop
dr.Close()
conn1.Close()
conn2.Close()

Open in new window


The problem is, that the UPDATE command times out and never executs properly. I'm not surprised, because I'm modifying the data while it's being read by the datareader. Surely some kind of locking taking place here.

What would be the best way to loop through data, work on it, and write the modified records back?

Thank you for your support,

Thomas
ASKER CERTIFIED SOLUTION
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Staudte

ASKER

Will this read the entire 200MB into the RAM of the server? (Sorry, not familiar with datatables, although I have already seen the code I need to handle them.)
Yes it would pull them all into memory.  Personally I would find a way to do all this on the server via a stored procedure but I don't know enough about [.. do some calculations ..] to determine if that's possible.

You could always opt to bring them through in batches if it becomes a RAM issue.  Make some alterations to your query or create a stored procedure that can pull a "page" of records at a time, make your adjustments, and update.
Avatar of Staudte

ASKER

Ok, I see. This code only runs once in a lifetime, does some updates (very complex - can't easily be done in a stored proc) and will never be needed again, so using 200MB of RAM will be ok - the server should have that much. I just wanted to understand what would happen.

Thanks for the quick reply - awesome!
You might try simply extending the connection timeout value which defaults to 30 seconds.
Avatar of Staudte

ASKER

Why should this help? If there's a locking issue, it will never be resolved inside the datareader loop, no matter how long I set the timeout value. I have - for a test - closed the datareader inside the loop just before executing the update, and the update ran smoothly. (Of course, it wouldn't be able to do another run of the loop with the datareader closed.)
I thought you might be having a timeout issue based on the size of the dataset.  If that's not the case then just go with the original suggestion :-)