Can you lock an entire table in SQL Server while two people are in it and then when one person is in it unlock that entire table?

Posted on 2011-10-06
Last Modified: 2012-05-12
If someone is editing data belonging to one table and another comes along and starts to edit other data belonging to that same table - what I would like to do is lock it read only for the second user - where he/she cannot change it - sort of what Excel does when two people open the same document - is that possible?    Then if the first user leaves - the locking is removed for the second user - where he/she can change it?

Sure there is a lot online for record-row level locking, column locking would slow performance and then you get into user-permisions.  But this is special to where you want the locking to occur only on the second user (or third, fourth) temporarily and return when there is one user only accessing the table.

Second question - should this not be done at database level but merely only software front-end?
Question by:stephenlecomptejr
    LVL 23

    Accepted Solution

    You can lock the entire table for update by using hints.  Something like this:

    SELECT Col1, Col2 
       FROM YourTable
        WHERE Col3 in (1, 2, 3);

    Open in new window

    LVL 67

    Assisted Solution

    The above will prevent anybody else from reading the table, as the "Intended Update" lock will not allow reads with "Intended Update" locks; else the lost update issue would arise (reading a set of old data, while they are changed by someone else, and then wanting to update those old records). However, it works if you catch the error generated (lock timeout, or deadlock), and then do the same select without the locks. That, however, would require you check the table regularly for being able to lock it again.

    A much better approach to lock a complete table is to maintain a control table. As soon as there is an entry for the table to view, you prohibit any update. The first "user" inserts the table with a flag, so you can identify he's the "owner", and able to update. This technique again requires checking, of the control table whenever an update is attempted.
    LVL 1

    Author Comment

    "This technique again requires checking, of the control table whenever an update is attempted."

    Is this done as a trigger in SQL server or done within the front-end software accessing sql server?
    LVL 67

    Expert Comment

    Front-End - it is application logic.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
    The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
    Via a live example, show how to shrink a transaction log file down to a reasonable size.
    Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now