tags:

views:

109

answers:

2

Is the following scenario possible in SQL Server using a single active connection?

Inside a READCOMMITED transaction I need to update one table without locking it. For example each time I execute a statement I increase a field in that table. This operation doesn't need to be rolled back in case the transaction fails. Also, this update shouldn't block other concurrent users which try to update the same position.

+1  A: 

You cannot perform an Update without putting locks on the table. This is to allow the Transaction isolation level of OTHER transactions to control whether they can "see" the changes made in the update. You can change the Transaction Isolation Level for the update, but this only affects the update session, (where you set it) controlling whether the update can "see" changes made in other sessions...

If you want OTHER Sql statements to be able to see the stuff you're doing in this update, as though it was not locked, you have to change the transaction isolation level on those other txs to Read Uncommited. (Careful about this... This isolation level can allow numerous inconsistencies into your database.)

Charles Bretana
A: 

You can't not lock a table during a write.

Do you mean that you want to write to a table, but then have the exclusive lock not persisted until commit/rollback?

If so, put the value(s) you need into a table variable (which is unaffected by rollback/commit) and defer the write to after the main transaction. Then, do a single update to push the value from the table variable.

Edit: SQL 2008 workaround, with other ideas later

gbn
tx, autonomous transactions are really interesting.