2

powerbuilderからのデータを使用して、テーブルにいくつかの挿入を行いたい。SQLシーケンスを使用して最後の既知のIDをインクリメントすることになると、少し迷います。

最大IDを見つける方法は知っていますが、シーケンスを使用して、新しいINSERTごとにIDが1ずつ増えるようにするにはどうすればよいですか?

これまでのところ、最大IDを見つける方法だけがあります。

SELECT MAX(id)
FROM table_name;

編集 データベースにOracleを使用しています

ありがとう。

4

3 に答える 3

3

If IDs' values of a table you are inserting data into come from an already existed in your schema sequence then you are welcome to keep using that sequence to insert new records.

If you want to create a new sequence that would generate numbers starting from the maximum value of ID column of your table then you can do the following

Create sequence:

create sequence Seq_Name;

Change the value sequence would start with

declare
  l_max_ID number;
  l_Temp_val number;
begin
  select max(ID) into l_max_ID
    from your_table;

  execute immediate 'alter sequence Seq_Name increment by ' || To_Char(l_Max_ID);

  select Seq_Name.currval into l_temp_val
    from dual;

  execute immediate 'alter sequence Seq_Name increment by ' || To_Char(1);
end; 

And use any of listed below approaches to get next value of the sequence.

Insert into your_table(id, [other_columns])
  values(Seq_name.nextval, [other_values]);

OR

create or replace trigger Tr_Name before insert on Your_Table_name
for each row
begin
  -- if you are using oracle prior to 11g then 
  select Seq_name.nextval into :new.id -- or any other column
    from dual;
  --     OR
  -- if your Oracle version is 11g onward you can simply assign 
  -- sequence's value to a new ID
  :new.id := Seq_Name.nextval;

end;
于 2012-09-24T14:15:57.493 に答える
2

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;
/
于 2012-09-24T14:08:49.607 に答える
2

これが、シーケンスの使用方法です。

22:05:18 HR@vm_xe> create table test_seq_tab(a number, b number);                                            

Table created.                                                                                               

Elapsed: 00:00:00.85                                                                                         
22:05:43 HR@vm_xe> create sequence test_seq start with 1 increment by 1;                                     

Sequence created.                                                                                            

Elapsed: 00:00:00.12                                                                                         
22:06:33 HR@vm_xe> insert into test_seq_tab select test_seq.nextval, rownum from dual connect by level <= 10;

10 rows created.                                                                                             

Elapsed: 00:00:00.11                                                                                         
22:06:40 HR@vm_xe> select * from test_seq_tab;                                                               

         A          B                                                                                        
---------- ----------                                                                                        
         1          1                                                                                        
         2          2                                                                                        
         3          3                                                                                        
         4          4                                                                                        
         5          5                                                                                        
         6          6                                                                                        
         7          7                                                                                        
         8          8                                                                                        
         9          9                                                                                        
        10         10                                                                                        

10 rows selected.                                                                                            

Elapsed: 00:00:00.10                                                                                         
22:06:50 HR@vm_xe> select test_seq.currval from dual;                                                        

   CURRVAL                                                                                                   
----------                                                                                                   
        10                                                                                                   

1 row selected.                                                                                              

Elapsed: 00:00:00.01                                                                                         
22:07:04 HR@vm_xe>                                                                                           

いいえselect max ...、必要ありません。このようにしていますか?

于 2012-09-24T14:09:31.053 に答える