Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stored Procedure for copying data from one table to another

I have pairs of tables in the format TABLE and TABLE_TWIN now

  • TABLE is the main table with lots of data
  • TABLE_TWIN is a table with the exact same fields with a little data (different data)

Now I would like to copy all rows from TABLE_TWIN to TABLE using a stored procedure. I have many such tables and could like the stored procedure to take the table name(s) as parameter(s) so that I can use the same procedure for each table pair. I do not want to write long INSERT statements because these tables have around 50 attributes each.

I am not good with PL/SQL so I need some help here.

Thanks!

like image 238
Virat Kadaru Avatar asked Dec 08 '22 05:12

Virat Kadaru


2 Answers

SQL is not so long... But if you prefer a procedure, here it is:

create or replace procedure table_copy(
  p_tab_from varchar2,
  p_tab_to   varchar2)
is
begin
  execute immediate 'insert into '||p_tab_to||' (select * from '||p_tab_from||')';
end;
like image 98
Egor Rogov Avatar answered Dec 09 '22 17:12

Egor Rogov


insert into table_twin (select * from table) 

should do it

like image 29
Klaus Byskov Pedersen Avatar answered Dec 09 '22 18:12

Klaus Byskov Pedersen