Locking objects in SQL Server

As my colleague James said in a recent blog post:

“If you share a database with other developers, chances are you’ve encountered a situation like this:

1. Alice starts editing an object.

2. Meanwhile, not realizing Alice is working on the object, Bob unwittingly edits and saves the same object.

3. Alice, not realizing Bob was working on the same thing, saves her changes.

Presto: Bob’s work is gone. And what’s more, Bob and Alice might not even notice. The error could make it all the way to production.”

It’s a familiar story. Two developers are working on a database, they’re using version control, and one of them unwittingly overwrites the others’ changes. I’ve heard first how frustrating this can be – we spoke to one developer who lost a week’s work when their database was deployed without critical changes that had been accidentally overwritten. Ouch.

A bit of background: In 2013, we released Source Control for Oracle 2, which included a feature to let users lock objects on Oracle databases so that other users can’t edit them. A while later, we released a free beta of SQL Lock, an add-in for Management Studio, to try and solve the same problem for SQL Server development teams. We’ve recently retired the SQL Lock beta and have rolled the feature into SQL Source Control. In case you’re not familiar with the latter, SQL Source Control is an add-in for SQL Server Management Studio that links your database to your version control system.

So, how do you use object locking? Simple. Once you’ve set it up and you’re ready to work on an object, just right-click it in the Object Explorer and select Lock this object.


When you’ve locked it, the object is marked with a padlock icon in the Object Explorer.


If someone else tries to save changes to the object, they’ll see an error explaining that it’s locked by you.

Of course, if you take a week off work or get hit by a bus, your team’s going to need to work on your locked objects without you. For that reason, anyone can unlock an object, even if they didn’t lock it themselves. This way the team can continue working on the object without any delays. We see the feature as not being a way to enforce rules, but something to help you to not tread on each other’s toes.

If you go to the Object locking tab you can see all of the objects which have been locked on that server – select the ones which you’d like to unlock and click Unlock selected objects.


All the locking is done on the server, so it works independently of your version control system.

If you’re more of a video person than a blog person then you can watch this walk-through video of the locking feature by Microsoft MVP Alessandro Alpi:

If you’d like to try object locking out you’ll need to install the latest version of SQL Source Control, which has just been released – hurrah!. Download it here and feel comforted that your work is in safe hands. Oh, and let me know what you think – if you have any feedback I’d love to hear it.

What’s next for SQL Source Control? Over the next 6 months you’ll see a lot of changes and improvements to SQL Source Control. We’ll be posting here soon about what’s coming up.