How to perform a row lock?

后端 未结 4 930
悲哀的现实
悲哀的现实 2020-12-10 06:42

I want to lock one record and then no one may make changes to that record. When I release the lock, then people may change the record.

In the meantime that a record

4条回答
  •  旧巷少年郎
    2020-12-10 06:57

    See this duplicate question on SO.

    Basically it's:

    begin tran
    
    select * from [table] with(holdlock,rowlock) where id = @id
    
    --Here goes your stuff
    
    commit tran
    

    Archive

    Something like this maybe?

    update t 
    set t.IsLocked = 1 
    from [table] t 
    where t.id = @id
    

    Somewhere in the update trigger:

    if exists (
    select top 1 1 
    from deleted d 
    join inserted i on i.id = d.id
    where d.IsLocked = 1 and i.RowVersion <> d.RowVersion)
    begin
    print 'Row is locked'
    rollback tran
    end
    

提交回复
热议问题