9

I want to select fields in a record of a table and update just one of these fields. How can I do?

I try this:

SELECT v.idvideo, v.title 
FROM video v  WHERE v.schedulingflag IS FALSE AND v.errorflag IS FALSE 
ORDER BY v.idvideo  LIMIT 1 FOR UPDATE ;

UPDATE video  SET schedulingflag = true;

But in this way it sets field "schedulingflag" true in all record!

1 Answer 1

16

The SELECT FOR UPDATE syntax tells PG that you're going to be updating those records and locks them against concurrent access. However you still need to issue the appropriate UPDATE call to change the particular records you've locked.

In this case, just use the same WHERE clause in your UPDATE, e.g:

UPDATE video  SET schedulingflag = true 
WHERE schedulingflag IS FALSE AND errorflag IS FALSE;
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, i want to lock the table, can you see my previous question? stackoverflow.com/questions/14707002/…
Note, that he's using a LIMIT 1 for the FOR UPDATE, this generally means that his where clause may return more than 1 record. Now, since I don't have his DDL for the table, he should use whatever his primary key is to do the update, if its idvideo, then he should use UPDATE video SET scheduleflag=true WHERE idvideo=?. The reason being is that even if he uses the same query (ORDER BY/LIMIT), he could STILL update a different record (a new id could be inserted before it)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.