[Solved] An Update statement in a loop


This can be done with a single update statement.

delete from question
   where id = 2;

with new_order as (
   select row_number() over (partition by survey_id order by question_no) as new_question_no,
          question_no as old_question_no, 
          id
   from question
) 
update question 
  set question_no = nq.new_question_no
from new_order nq
where nq.id = question.id
  and survey_id = 44;

commit;

Here is an SQLFiddle example: http://sqlfiddle.com/#!6/0a1e7/1

0

solved An Update statement in a loop