Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your procedure doesn't work the way you think it should.. If your sequence last value was 10, then you are altering the sequence to increment by -10 every time it is called. I am guessing the first time you execute it, you get an ORA-08004 because your minvalue is probably 1 and it would be trying to return 0 (which isn't allowed). Even if that didn't error, the next time you called it would, as it would try to return -10 in my example. What I believe you really want is:</p> <pre><code>CREATE OR REPLACE PROCEDURE reset_seq (p_seq_name IN VARCHAR2) IS l_val NUMBER; BEGIN -- Get Current Value of Sequence EXECUTE IMMEDIATE 'select ' || p_seq_name || '.nextval from dual' INTO l_val; -- Alter to sequence to allow to go to 0 and decrease by current value EXECUTE IMMEDIATE 'alter sequence ' || p_seq_name || ' minvalue 0 increment by -' || l_val; -- Get value from sequence again (should set seq to 0) EXECUTE IMMEDIATE 'select ' || p_seq_name || '.nextval from dual' INTO l_val; -- Alter sequence to increase by 1 again EXECUTE IMMEDIATE 'alter sequence ' || p_seq_name || ' increment by 1'; END; </code></pre> <p>This allows the sequence to be 0 (which you need if you want the next call to return 1), sets it to 0, then changes it back to increment by 1 with each successive call. However, it is probably much easier to just drop and recreate the sequence.</p> <p>The real question though, is why you would ever want to do this. This looks like bad design. A sequence is just supposed to return a unique number. Nothing more, nothing less. There should be no meaning behind the number, and it sure seems like you are trying to assign meaning here. Sequences don't guarantee your rows will be inserted in order and don't guarantee there won't be gaps, they just provide a unique number. Concatenating the year on the end of this unique number makes this design more suspect.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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