0

5 つのフィールドを持つテーブルがあります。5 つの列のうち 1 つが PK です。私の要件は、いくつかの列(重複していないがPKではない)に基づいて行を取得する必要があり、返されたすべての結果に対して、新しいPKを割り当てて保存する必要があることです。

テーブルに 10 個のレコードがあるとします。特定の列に基づいて10個のレコードを取得した場合。10 個のレコードすべてに新しい PK を割り当てて保存する必要があります。最後に、テーブルには 20 のレコードがあります。この操作を行う単一の SQL クエリはありますか?

ありがとう!

4

3 に答える 3

1
--  create a sequence to manage the primary keys
create sequence key_sequence;

--  i don't know what data you want in your table
create table tempTable (
myKey int primary key,
myValue varchar(12))

--  create four rows of arbitrary data, they will get primary keys of 1,2,3 and 4
insert into tempTable values (key_sequence.nextval, 'eggs')
insert into tempTable values (key_sequence.nextval, 'bacon')
insert into tempTable values (key_sequence.nextval, 'chips')
insert into tempTable values (key_sequence.nextval, 'salad')

--  you can see the 4 rows
select * from tempTable

--  select all four rows (as no where clause) and re-insert them into the table
--  the sequence will take care of allocating new primary keys
insert into tempTable
select  key_sequence.nextval, myValue 
from    tempTable

--  now you can see eight rows in the table
select * from tempTable
于 2012-07-05T10:46:21.517 に答える
0

IF col1 は PK (自動インクリメント列用)

insert into table (col1, col2, col3, col4, col5) 
    select null, col2, col3, col4, col5 
    from table 
    where col2 = 'anyvalue' and more conditions... ;
于 2012-07-05T10:43:31.990 に答える
0

私があなたを正しく理解したかどうかはわかりませんが、これであなたが必要とすることはできますか?

あまり詳しく説明していないので、空欄を埋める必要があります。

INSERT INTO <table> (
   pk_col,
   col2,
   col3,
   col4,
   col5
)
   SELECT <new_pk>,
          col2,
          col3,
          col4,
          col5
     FROM <table>
    WHERE <your search criteria>;

それが役に立てば幸い...

于 2012-07-05T10:43:45.833 に答える