各种锁的介绍:https://www.cnblogs.com/bdqczhl/p/13785567.html
典型用法:
--在第1个连接中执行以下语句
BEGIN TRAN;
UPDATE SiteMsg_Msg
SET Title = '公告'
WHERE ID = '55BA3F4B-184C-466B-995F-05749BD55A2B';
WAITFOR DELAY '00:00:10';
--等待10秒
COMMIT TRAN;
--在第2个连接中执行以下语句
SELECT *
FROM SiteMsg_Msg
WHERE ID = '832A1C27-FF79-4040-A3A8-7C7E97ACE208';
查其它行,秒开
SELECT *
FROM SiteMsg_Msg
WHERE ID = '55BA3F4B-184C-466B-995F-05749BD55A2B';
查被锁的行,需要等待
上面的是update语句会自动锁行(锁住被update的行)
如果是select语句,需要手动加上with(xlock,ROWLOCK)
BEGIN TRAN;
SELECT *
FROM SiteMsg_Msg with(xlock,ROWLOCK)
WHERE ID = '55BA3F4B-184C-466B-995F-05749BD55A2B';
WAITFOR DELAY '00:00:10';
COMMIT TRAN;
这行被锁住
BEGIN TRAN;
SELECT *
FROM SiteMsg_Msg
WHERE ID = '55BA3F4B-184C-466B-995F-05749BD55A2B';
WAITFOR DELAY '00:00:10';
COMMIT TRAN;
这行不会被锁住