sql-server-2008rowlocking

Are data rows locked when editing them in SQL Server Management Studio 2008?


When I right-click on a table in SQL Server Management Studio and choose 'Edit top 200 rows', at what point, if ever, would the data that I'm looking at be locked?

A colleague of mine stated that when viewing data in this way, the data rows can be locked (I think when he said if you put a cursor on a row and start editing data).

Is this the only time the data can be locked in this context?


Solution

  • It's not true. Run this script in one window (and leave it running):

    create table Tx (
        ID int not null,
        Val1 varchar(20) not null
    )
    go
    insert into Tx (ID,Val1)
    select 1,'abc'
    go
    set nocount on
    while 1=1
    begin
        update Tx set Val1 = CASE WHEN Val1='abc' then 'def' else 'abc' end
        RAISERROR('Updated',10,1) WITH NOWAIT
        WAITFOR DELAY '00:00:05'
    end
    

    Every 5 seconds, it flips the value of the Val1 column, and prints Updated in the messages tab (assuming you're set to "results in grid").

    Then, in another instance of SSMS, open the Tx table and start editing the single row in there. Observe that the script continues to perform updates.