0

Is there any way by which we can lock only single row of the table (IN SQL SERVER 2008)

I am trying something like this

BEGIN TRAN

select TOP 1 from [TableName] with (ROWLOCK,UPDLOCK)

COMMIT

But seems like SQL server is ignoring ROWLOCK and acquiring IU lock on page and IX on Table.

Your help will be appreciated :)

4

3 回答 3

3
update Production.Location with (ROWLOCK)
   set CostRate = 100.00
 where LocationID = 1  

当您想要更新该记录时使用行锁。

于 2013-11-13T09:37:01.830 回答
2

使用 UPDLOCK 提示,您正在请求 UPDATE 锁,这就是为什么您的 SELECT 卡在等待上一个事务完成的原因。您应该指定 HOLDLOCK 但不指定 UPDLOCK:

select TOP 1 * from t1 with (ROWLOCK, HOLDLOCK)

这将保留一个共享锁并让其他事务选择数据,但不更新它:如果另一个会话尝试更新被此共享锁锁定的行,它将不得不等待您释放锁(结束您的事务)

您可以使用此查询查看锁:

SELECT request_session_id ,
    resource_type ,
    DB_NAME(resource_database_id) AS DatabaseName ,
    OBJECT_NAME(resource_associated_entity_id) AS TableName ,
    request_mode ,
    request_type ,
    request_status
FROM sys.dm_tran_locks AS L
JOIN sys.all_objects AS A ON L.resource_associated_entity_id = A.object_id
于 2013-11-13T16:10:21.350 回答
2
select TOP 1 from [TableName] with (ROWLOCK)
于 2013-11-13T11:43:14.367 回答