I have a C# project which connects to a TSQL database. The project runs multiple sequential update statements on a single table, eg.:
private void updateRows() {
string update1 = "UPDATE WITH (ROWLOCK) table SET ... WHERE ...;"
string update2 = "UPDATE WITH (ROWLOCK) table SET ... WHERE ...;"
string update3 = "UPDATE WITH (ROWLOCK) table SET ... WHERE ...;"
// execute updates one after the other
}
Ideally, these statements will be batched to avoid making multiple round-trips to/from the database:
string update = "
UPDATE WITH (ROWLOCK) table SET ... WHERE ...;
GO
UPDATE WITH (ROWLOCK) table SET ... WHERE ...;
GO
UPDATE WITH (ROWLOCK) table SET ... WHERE ...;
GO
";
My question is, if the statements are batched, does this increase the chance of deadlock errors occurring due to table scans?
As there is less time between each statement, I imagine that this could increase chances of deadlocks as one update could introduce a row or page lock, which may not be released by the time the next statement is executed. However if the update statements were not batched, then there is more time for row or page locks to be released between each update statement, therefore less chance of deadlocks occurring.