T-SQL: Lock a table manually for some minutes [duplicate] T-SQL: Lock a table manually for some minutes [duplicate] sql-server sql-server

T-SQL: Lock a table manually for some minutes [duplicate]


If I understand your question correctly, it sounds like you would like to force a table lock for purposes of testing. To do that with SQL Server, you can create and run a stored procedure that will cause an exclusive table lock on the table of interest. While that lock is active, you can run the program you want to test.

The following stored procedure should lock your table for 2 minutes. You can change the delay parameter to suit your needs.

BEGIN TRAN  SELECT TOP (1) 1 FROM TABLE WITH (TABLOCKX)WAITFOR DELAY '00:02:00' ROLLBACK TRAN   GO 

A general approach for doing this was described in the stackoverflow post below. The MSDN link provides some additional information on the WAITFOR statement.

Original stackoverflow post

Supplementary MSDN post

Hope it helps.