Sql 如何通过更新两个字段来插入记录?

Sql 如何通过更新两个字段来插入记录?,sql,oracle,Sql,Oracle,我有一张有5个字段的桌子。5列中有一列是PK。我的要求是我需要根据某个列(非重复但非主键)获取行,对于所有返回的结果,我需要分配新的主键并保存 如果我的表中有10条记录。如果我根据某一列得到10条记录。我需要为所有10条记录分配新的PK并保存。最后,表中将有20条记录。是否存在执行此操作的单个SQL查询 谢谢 如果col1是PK(对于自动增量列) 我不确定我是否理解正确,但这能满足你的需要吗 由于你没有给出太多的细节,你必须填空 INSERT INTO <table> ( pk

我有一张有5个字段的桌子。5列中有一列是PK。我的要求是我需要根据某个列(非重复但非主键)获取行,对于所有返回的结果,我需要分配新的主键并保存

如果我的表中有10条记录。如果我根据某一列得到10条记录。我需要为所有10条记录分配新的PK并保存。最后,表中将有20条记录。是否存在执行此操作的单个SQL查询

谢谢

如果col1是PK(对于自动增量列)


我不确定我是否理解正确,但这能满足你的需要吗

由于你没有给出太多的细节,你必须填空

INSERT INTO <table> (
   pk_col,
   col2,
   col3,
   col4,
   col5
)
   SELECT <new_pk>,
          col2,
          col3,
          col4,
          col5
     FROM <table>
    WHERE <your search criteria>;
插入到(
pk_col,
col2,
col3,
col4,
可乐
)
选择,
col2,
col3,
col4,
可乐
从…起
哪里

希望它有帮助…

ys这是可能的,但为此,您需要显示您的pk生成逻辑和表模式。如果您确定没有冲突,您可以使用序列并使用序列生成pk。\u name.NEXTVAL
--  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
--  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