title: UPDATE with ORDER BY
body: <p>Need to &quot;tie&quot; <code>UPDATE</code> with <code>ORDER BY</code>. I'm trying to use cursors, but get the error:</p>
  <blockquote>
  <pre><code>cursor &quot;cursupd&quot; doesn't specify a line,
  SQL state: 24000
  </code></pre>
  </blockquote>
  <p>Code:</p>
  <pre><code>BEGIN;
      DECLARE cursUpd CURSOR FOR SELECT * FROM &quot;table&quot; WHERE &quot;field&quot; = 5760 AND &quot;sequence&quot; &gt;= 0 AND &quot;sequence&quot; &lt; 9 ORDER BY &quot;sequence&quot; DESC;
      UPDATE &quot;table&quot; SET &quot;sequence&quot; = &quot;sequence&quot; + 2 WHERE CURRENT OF cursUpd;
      CLOSE cursUpd;
  COMMIT;
  </code></pre>
  <p>How to do it correctly?</p>
  <h3>UPDATE 1</h3>
  <p>Without cursor, when I do like this:</p>
  <pre><code>UPDATE &quot;CableLinePoint&quot; AS &quot;t&quot;
  SET &quot;sequence&quot; = t.&quot;sequence&quot; + 2
  from (
      select max(&quot;sequence&quot;) &quot;sequence&quot;, &quot;id&quot;
      from &quot;CableLinePoint&quot;
      where
          &quot;CableLine&quot; = 5760
      group by &quot;id&quot;
      ORDER BY &quot;sequence&quot; DESC
  ) &quot;s&quot;
  where &quot;t&quot;.&quot;id&quot; = &quot;s&quot;.&quot;id&quot; and &quot;t&quot;.&quot;sequence&quot; = &quot;s&quot;.&quot;sequence&quot;
  </code></pre>
  <p>I get the unique error. So, need to update from the end rather than from the beginning.</p>
  <h3>UPDATE 2</h3>
  <p>Table:</p>
  <pre><code>id|CableLine|sequence
  10|    2    |    1
  11|    2    |    2
  12|    2    |    3
  13|    2    |    4
  14|    2    |    5
  </code></pre>
  <p>Need to update (increase) the field &quot;sequence&quot;. &quot;sequence&quot; have &quot;index&quot; type, so cannot be done:</p>
  <pre><code>UPDATE &quot;table&quot; SET &quot;sequence&quot; = &quot;sequence&quot; + 1 WHERE &quot;CableLine&quot; = 2
  </code></pre>
  <p>When &quot;sequence&quot; in the row with <code>id = 10</code> is incremented by <code>1</code> I receive an error that another row with <code>&quot;sequence&quot; = 2</code> already exists.</p>