Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I alter a sequence in dynamic SQL?

I'm trying to create a script to migrate data from one DB to another. One thing I'm not currently able to do is set the nextval of a sequence to the nextval of a sequence in another DB.

I got the difference in values from user_sequences and generated the following dynamic SQL statements:

execute immediate 'alter sequence myseq increment by 100';
execute immediate 'select myseq.nextval from dual';
execute immediate 'alter sequence myseq increment by 1';

commit;

But nothing happens. What am I missing? If I run the same statements outside the procedure, they work fine:

alter sequence myseq increment by 100;
select myseq.nextval from dual;
alter sequence myseq increment by 1;

commit;

EDIT: Apologies to all for not being clear. I'm actually altering the sequence in the same DB. I'm only getting the value to be set from a remote DB. Perhaps it was unnecessary to mention the remote DB as it doesn't affect things. I only mentioned it to explain what my goals were.

Step 1. I get the nextval of the sequence from a remote DB.

select (select last_number
        from dba_sequences@remoteDB
        where upper(sequence_name) = upper(v_sequence_name)) - (select last_number
                                                                from user_sequences
                                                                where upper(sequence_name) = upper(v_sequence_name)) increment_by
from dual;    

Step 2. I generate dynamic SQL statements with this value:

execute immediate 'alter sequence myseq increment by 100';
execute immediate 'select myseq.nextval from dual';
execute immediate 'alter sequence myseq increment by 1';

commit;

No error was raised, but nothing happened. When I wrote the SQL statements with DBMS_OUTPUT.PUT_LINE and ran them outside they worked.

like image 979
Zesty Avatar asked Apr 30 '12 11:04

Zesty


People also ask

How do you modify a sequence in SQL?

Sequences objects are created by using the CREATE SEQUENCE statement. Sequences are integer values and can be of any data type that returns an integer. The data type cannot be changed by using the ALTER SEQUENCE statement. To change the data type, drop and create the sequence object.

How do you modify an existing sequence?

Use the ALTER SEQUENCE statement to change the increment, minimum and maximum values, cached numbers, and behavior of an existing sequence. This statement affects only future sequence numbers.

Can sequence be modified?

The data type of a sequence cannot be changed. Instead, you must drop the current sequence and then create a sequence specifying the new data type. After restarting a sequence or changing to CYCLE, it is possible to generate duplicate sequence numbers.

How do you reset a sequence value in SQL?

In the SSMS GUI, navigate to Database Folder – [Database Name] – Programmability Folder – Sequences Folder. 4. Select the “Restart sequence” checkbox. You can enter the value you want to reset the sequence to, or keep the default value.


2 Answers

Here is some code which dynamically sets a sequence to a new (higher) value. I have written this so it will work for any sequence in your schema.

create or replace procedure resync_seq
    (p_seq_name in user_sequences.sequence_name%type)
is
    local_val pls_integer;
    remote_val pls_integer;
    diff pls_integer;
begin
    execute immediate 'select '|| p_seq_name ||'.nextval from dual'
           into local_val;
    select last_number into remote_val
    from user_sequences@remote_db
    where sequence_name = p_seq_name ;
    diff := remote_val - local_val;

    if diff > 0
    then
        execute immediate 'alter sequence  '|| p_seq_name ||' increment by ' ||to_char(diff);
        execute immediate 'select '|| p_seq_name ||'.nextval from dual'
           into local_val;
        execute immediate 'alter sequence  '|| p_seq_name ||' increment by 1';
    end if;

end;

The procedure doesn't need a COMMIT because DDL statements issue an implicit commit (two in fact).

You can execute it and see the synced value like this (in SQL*PLus):

exec resync_seq('MYSEQ')
select myseq.currval
from dual

Incidentally, the only way to reset a sequence (to its original starting value or a different lower value) is dropping and re-creating the sequence.


In 18c Oracle added a RESTART capability to ALTER SEQUENCE. The straightforward option ...

alter sequence myseq restart;

...resets the sequence to the value specified by the START WITH clause in the original CREATE SEQUENCE statement. The other option allows us to specify a new starting point:

alter sequence myseq restart start with 23000;

Excitingly this new starting point can be ahead or behind the current value (within the usual bounds of a sequence).

The one snag is that this new capability is undocumented (only for Oracle's internal usage) and so we're not supposed use it. Still true in 20c. The only approved mechanism for changing a sequence's value is what I outlined above.

like image 159
APC Avatar answered Sep 19 '22 11:09

APC


I wasn't quite able to understand what you mean, but here is a wild guess:

I don't see it in your code, but you're talking about executing DDL (CREATE, ALTER etc.) on another database, so I assume you are using Database Links. It is not possible to use Database Links to execute DDL on another database. You might want to consider that.


After the information you provided, this might be what you need. And if you want to set the current value of the sequence, you can't, according to this documentation, you need to drop/create:

declare
  ln_lastNumber DBA_SEQUENCES.LAST_NUMBER%type;
  lv_sequenceName DBA_SEQUENCES.SEQUENCE_NAME%type := 'MYSEQ';
begin
  select LAST_NUMBER
  into ln_lastNumber
  from DBA_SEQUENCES--or @remote_db;
  where
    --Your predicates;

  execute immediate 'drop sequence ' || lv_sequenceName;
  execute immediate 'create sequence ' || lv_sequenceName || ' starts with ' || ln_lastNumber;
exception
  when no_data_found then
    dbms_output.put_line('No sequence found!'); -- Or log somehow.
    raise;
  when others then
    raise;
end;
like image 36
Erkan Haspulat Avatar answered Sep 18 '22 11:09

Erkan Haspulat