Oracle では、シーケンスの次の値を取得するには、.nextval を使用します。
例 :
select my_sequence.nextval from dual;
データをインポートしていて、10000 までの ID がすでに使い果たされているとしましょう。シーケンスを変更して、その数だけインクリメントすることができます。これは DDL であるため、動的 SQL を使用する必要がある場合があります。
declare
l_current_max_value number;
l_dummy number
begin
select max(id)
into l_current_max_value
from my_table;
for i in 1..l_current_max_value loop
l_dummy := l_current_max_value.nextval; --bouncing the sequence,
--another option is to recreate it.
end loop;
end;
/
注: これは、現在のシーケンスがまったく使用されていないことを前提としています。データを含む既存のテーブルにデータをインポートする場合、同じ行に沿ってより多くの作業が必要になるため、両方のテーブルで共通の ID について考える必要があります。
編集:「current_max_number を my_sequence に割り当てるにはどうすればよいですか」
すべてのインポートが完了したら、.nextval を使用して ID を取得できます。例えば。
create or replace procedure new_product(
i_sku in number,
i_name in varchar2(100)
)
as
begin
insert into new_product (id, sku, name)
values (product_seq.nextval, i_sku, i_name);
commit;
end;
/
または、さらに処理するために変数に入れることができます..
declare
l_next_id number;
begin
select my_sequence.nextval
into l_next_id
from dual;
--further processing
end;
/