Oracle复制行记录的小技巧

需求:

对于数据库的某些表,我们常要复制其对应的某条记录,新纪录与老记录仅有某一两个字段值不一样,其他完全一样。寻找简单的方案,而不是逐个字段拼Sql。

解决方案1:

begin
  for rs in (select * from dept where deptno=40)
  loop
    rs.deptno:=50;
    insert into dept values rs;
  end loop;
end;
/

解决方案2:

declare
  cursor dept_cursor is select * from dept where deptno=40;
begin
  for dept_rs in dept_cursor
  loop
    exit when dept_cursor%notFound;
    dept_rs.deptno:=null;
    insert into dept values dept_rs;
  end loop;
end;
/

备注:

查询的 表名 与插入的 表名 可以不一样,例如,查询:descartes,插入descartes_history

若有更好方法,请指教。

原文地址:https://www.cnblogs.com/advocate/p/1804677.html