Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are several problems with your initial approach. While the selected answer correctly provides a way to determine the current value of the sequence is does not address these problems:</p> <ol> <li>The value of the sequence might have changed between the call to NEXTVAL and CURRVAL. This will lead to a hard to detect bug and there is a possibility that you will get a value used by a different session. <strong>Use the returning clause in the insert statement to retrieve the actual inserted value.</strong></li> <li>Your variable names are the same as your column names. This will lead to hard to detect bugs in queries embedded inside PL/SQL blocks. <strong>Make sure your variables are named differently - you can prefix them from the type name like v_userid instead of userid.</strong></li> <li><p>SELECT statement inside an Oracle PL/SQL block requires an INTO clause. <strong>Example:</strong></p> <p>SELECT userid INTO v_userid FROM bs_orders WHERE ono = orderNumberSEQ;</p></li> <li><p>The sub-query for current_timestamp is redundant. <strong>You can use plain CURRENT_TIMESTAMP in place of the sub-query to achieve the same result.</strong></p></li> <li><p>Instead of manually providing the column type. <strong>Anchor it to the exact table type using %type notation.</strong></p> <p>v_userid bs_orders.userid%type;</p></li> </ol> <p>The following code addresses all 5 issues.</p> <pre><code>DECLARE v_userid bs_orders.userid%type; -- anchoring the type BEGIN INSERT INTO bs_orders(userid , ono , timepurchased) VALUES('lilith', orderNum_seq.NEXTVAL, CURRENT_TIMESTAMP) RETURNING userid INTO v_userid; -- instead of currval and an additional select -- do whatever you want with v_userid here END; / </code></pre>
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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