sqlserver 鎖定一行資料,禁止讀取,修改和刪除,sqlserver一行

來源:互聯網
上載者:User

sqlserver 鎖定一行資料,禁止讀取,修改和刪除,sqlserver一行

sql 事務中 有時候需要鎖定某行,等事物完成後 釋放此行。


使用 with(ROWLOCK,UpdLock)   鎖定當前行。禁止讀取,修改和刪除


a 事務

ALTER PROCEDURE [dbo].[asuo]

AS
BEGIN


 begin tran
SELECT TOP 1 * FROM dbo.JYQ_HLT_Order with(ROWLOCK,UpdLock)  WHERE orderID=321878
UPDATE dbo.JYQ_HLT_Order SET orderBillingstatus=13   WHERE orderID=321878
SELECT TOP 1 * FROM dbo.JYQ_HLT_Order  WHERE orderID=321878
waitfor delay '00:00:10'
 COMMIT TRAN
END


b 事務


ALTER PROCEDURE [dbo].[bsuo]

AS
BEGIN
 begin tran
UPDATE dbo.JYQ_HLT_Order SET orderBillingstatus=14   WHERE orderID=321878
SELECT TOP 1 * FROM dbo.JYQ_HLT_Order  WHERE orderID=321878
 COMMIT TRAN
END



b事務等待 a事務 COMMIT TRAN 後 才會執行。

with(ROWLOCK)任何作用

with(ROWLOCK,UpdLock)   必須組合使用才可以生效

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.