How do you lock tables in SQL Server 2005, and should I even do it?

后端 未结 7 1973
醉话见心
醉话见心 2021-02-19 15:35

This one will take some explaining. What I\'ve done is create a specific custom message queue in SQL Server 2005. I have a table with messages that contain timestamps for both a

7条回答
  •  青春惊慌失措
    2021-02-19 16:17

    You want to wrap your code in a transaction, then SQL server will handle locking the appropriate rows or tables.

    begin transaction
    
    --Grab the next message id
    declare @MessageId uniqueidentifier
    set @MessageId = (select top(1) ActionMessageId from UnacknowledgedDemands);
    
    --Acknowledge the message
    update ActionMessages
    set AcknowledgedTime = getdate()
    where ActionMessageId = @MessageId
    
    commit transaction
    
    --Select the entire message
    ...
    

提交回复
热议问题