Solved

How to make sure the row in is not locked in WHERE clause?

Posted on 2014-04-08
6
186 Views
Last Modified: 2014-04-09
Here is my query. How do I make sure that row is NOT locked by anyone else? If someone has locked it then it should not return it. It should return me next top ullocked record.

SELECT TOP 1 Id FROM XYZ.
0
Comment
Question by:GouthamAnand
6 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39986112
Are you talking locked as in DB Lock?

If so then the query will wait until whoever else is done with the table, possibly timing out before the lock is released.

If you're talking about your own Lock column:
select top 1 id from XYZ where lock = 0

What exactly are you trying to do?
0
 
LVL 10

Expert Comment

by:PadawanDBA
ID: 39986142
So I have to ask...  What is the use case for this?  This is a rather in depth request (undocumented traceflag and possibly dmv) and there may be better options.
0
 

Author Comment

by:GouthamAnand
ID: 39986245
I need a TOP 1 record which has not been locked by ANYONE ELSE.

Several users working on the applicaton. And when a user opens a record on UI , it locks the record.

I want to select the top 1 record whish has not been locked by any other user.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 39986252
I would create your own lockedby column in the application then.

Set the lockedby column when you enter the record to the user
set the lockedby column to null when you leave the record

then you could do
select top 1 id from XYS where lockedby is null
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 39986586
>> when a user opens a record on UI , it locks the record. <<

What UI?  And how specifically does it "lock the record"?

Sorry, but how you're doing this really matters to how to properly answer this q.
0
 

Author Closing Comment

by:GouthamAnand
ID: 39989065
Thanks for all the responses.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this article I will describe the Detach & Attach 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.
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

8 Experts available now in Live!

Get 1:1 Help Now