Lock SQL table for a period of time Lock SQL table for a period of time sql sql

Lock SQL table for a period of time


In SQL Server Management Studio, run this TSQL script (set timeout to whatever suits you)

SET TRANSACTION ISOLATION LEVEL SERIALIZABLEGOBEGIN TRANSACTIONUPDATE tableSET ColumnName = ColumnNameWAITFOR DELAY '00:02:00'  -- 2 minutes   hh:mm:ss-- Run your test while table is locked and delay is counting downROLLBACK TRANSACTION


Using another session,

  1. Do a BEGIN TRANSACTION

  2. Call UPDATE table set columnName = columnName

  3. Run your test

  4. Verify your results

  5. COMMIT TRAN / ROLLBACK TRAN the transaction