Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

SQL Server - Update order by limit

Posted on 2015-02-20
Last Modified: 2015-03-05
Hi Experts,

With SQL Server, how does one do something like this?

Update tasks set Status=1 WHERE Status=0 ORDER BY otherColumn LIMIT 1

I've seen this done as a with statement with a select- but this doesn't seem to lock the row - and I need to lock the row...

Question by:thready
LVL 20

Accepted Solution

Russ Suter earned 334 total points
ID: 40621818
The ORDER BY clause is not valid with an update statement. What exactly are you trying to accomplish?
LVL 13

Expert Comment

ID: 40621823
I'm not familiar with the LIMIT, so I can't help you unless you explain a little further what you're trying to do.

Author Comment

ID: 40621834
I have various machines competing to mark N rows in tasks as reserved (as well as their machine name).  I'm trying to avoid collisions because various machines will periodically try to do this...  I guess I need an embedded select statement... but if I do it this way- will I get deadlock errors?
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

LVL 20

Assisted Solution

by:Russ Suter
Russ Suter earned 334 total points
ID: 40621851
I'd be more worried about concurrency errors than deadlock errors. What is your record locking model? Optimistic, pessimistic, other?

Author Comment

ID: 40621873
it's LockReadOnly...  I don't know anything about this, can you explain?

Author Comment

ID: 40621886
I read the docs- I get it- but since it's LockReadonly, I think my problem is with deadlock, not concurrency, right?
LVL 32

Assisted Solution

by:Daniel Wilson
Daniel Wilson earned 166 total points
ID: 40622227
That syntax is really close to what MySQL supports, but MS SQL Server doesn't.  If your WITH clause gets it for you, good.

Otherwise you may need a stored procedure.

create procedure SetStatus
 declare @ID int
 Set @ID = (Select top 1 ID from tasks where Status = 0 ORDER BY otherColumn)
 Update tasks set Status=1 WHERE ID=@ID

Open in new window

The procedure makes it a transaction which, I think, will take care of your concurrency issues.

Author Closing Comment

ID: 40648442
Thanks everyone

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

856 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