Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Server - Update order by limit

Posted on 2015-02-20
8
Medium Priority
?
267 Views
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...

Thanks,
Mike
0
Comment
Question by:thready
8 Comments
 
LVL 20

Accepted Solution

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

Expert Comment

by:LIONKING
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.
0
 
LVL 1

Author Comment

by:thready
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?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Assisted Solution

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

Author Comment

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

Author Comment

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

Assisted Solution

by:Daniel Wilson
Daniel Wilson earned 664 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
AS
Begin
 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
END

Open in new window


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

Author Closing Comment

by:thready
ID: 40648442
Thanks everyone
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

773 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