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

南楼画角 提交于 2019-11-30 00:06:27
Marty

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 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!