In SQL Server 2012, I am explicitly locking a table as listed below:
Reference: How to explicitly lock a table in Microsoft SQL Server (looking for a hack - uncooperative client)
While this lock is happening, I am running a SELECT query with NOLOCK on that table. But the query is not returing the data untill I stop the Window 1 and wait for some more time.
Why isn't NOLOCK
working as expected?
--Window 1
DECLARE @TranName VARCHAR(300)
SET @TranName = 'MyTran';
BEGIN TRANSACTION @TranName
DECLARE @Current INT
SET @Current = 0
DECLARE @LoopCount INT
SET @LoopCount = 1;
WHILE @Current < 1
BEGIN
SET @LoopCount = @LoopCount+1;
PRINT @LoopCount
ALTER TABLE DBATCPH ADD LockTest INT
ALTER TABLE DBATCPH DROP COLUMN LockTest
WAITFOR DELAY '00:01';
END
GO
Go
--Window 2
SELECT TOP 1 * FROM DBATCPH NOLOCK