Note that there are some explanatory texts on larger screens.

plurals
  1. POSelecting top n Oracle records with ROWNUM still valid in subquery?
    primarykey
    data
    text
    <p>I have the following FireBird query:</p> <pre><code>update hrs h set h.plan_week_id= (select first 1 c.plan_week_id from calendar c where c.calendar_id=h.calendar_id) where coalesce(h.calendar_id,0) &lt;&gt; 0 </code></pre> <p>(Intention: For records in <code>hrs</code> with a (non-zero) <code>calendar_id</code> take <code>calendar.plan_week_id</code> and put it in <code>hrs.plan_week_id</code>)</p> <p>The trick to select the first record in Oracle is to use <a href="http://www.orafaq.com/faq/how_does_one_select_the_top_n_rows_from_a_table" rel="nofollow noreferrer">WHERE ROWNUM=1</a>, <strong>and</strong> if understand correctly <a href="https://stackoverflow.com/questions/15590215/rownum-issue-in-oracle-query">I do not have to use ROWNUM in a separate outer query because I 'only' match ROWNUM=1</a> - thanks SO for suggesting <em>Questions that may already have your answer</em> ;-)</p> <p>This would make it</p> <pre><code>update hrs h set h.plan_week_id= (select c.plan_week_id from calendar c where (c.calendar_id=h.calendar_id) and (rownum=1)) where coalesce(h.calendar_id,0) &lt;&gt; 0 </code></pre> <p>I'm actually using the 'first record' together with the selection of only <strong>one</strong> field to guarantee that I get <strong>one value</strong> back which can be put into <code>h.plan_week_id</code>.</p> <p>Question: <strong>Will the above query work under Oracle as intended?</strong></p> <p>Right now, I do not have a filled Oracle DB at hand to run the query on.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload