Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move rows between tables using single statement in Oracle

Tags:

sql

oracle

Due to stupid legacy system limitations I am trying to write the following query using one single statement:

insert into dbo.mytable_archive 
  select * 
    from dbo.mytable 
   where date < trunc(sysdate) - 14;

delete from dbo.mytable 
 where date < trunc(sysdate) - 14;

Using the power of Google I find that this seems possible in many other databases using the RETURNING clause i Postgres or OUTPUT clause in SQLServer but I am unable to find an equivalent solution for Oracle (V12).

Any idea for a workaround?

like image 214
hirolau Avatar asked Mar 10 '23 18:03

hirolau


1 Answers

In case your statement runs around midnight and may take longer than 1 second you should better do this:

create or replace procedure move_to_arch as
   theDate DATE := trunc(sysdate) - 14;
begin
insert into dbo.mytable_archive 
  select * 
    from dbo.mytable 
   where date < theDate ;

delete from dbo.mytable 
 where date < theDate ;
commit;
end;
/
like image 82
Wernfried Domscheit Avatar answered Mar 13 '23 07:03

Wernfried Domscheit