• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

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

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.

1 Solution
Kyle AbrahamsSenior .Net DeveloperCommented:
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?
PadawanDBAOperational DBACommented:
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.
GouthamAnandAuthor Commented:
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.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Kyle AbrahamsSenior .Net DeveloperCommented:
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
Scott PletcherSenior DBACommented:
>> 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.
GouthamAnandAuthor Commented:
Thanks for all the responses.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now