3

I have SQL Select query with where clauses. For e.g

select * from table where status = 1

And how can I update single column with selected rows simultaneously while selecting? I want to mark selected rows, to avoid reselect on the next loop. Something like:

select * from table where status = 1; update table set proc = 1 where id in (select id from table where status = 1)

But this query will not return results.

1
  • 1
    There is only one table involved? BTW: don't name your tables table; invent some other meta-syntactic names. Commented Jun 13, 2012 at 11:13

1 Answer 1

3

Use the returning clause:

update table 
    set proc = 1 
where id in (select id from table where status = 1)
returning *;

(Btw: I assume the inner select is not actually selecting from the same table, because then the statement does not really makes sense as it could be rewritten with a simple where stauts = 1)

Sign up to request clarification or add additional context in comments.

Comments

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.