oracle 将数据从一张表复制到另一张表的存储过程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1842672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 19:35:19  来源:igfitidea点击:

Stored Procedure for copying data from one table to another

oraclestored-proceduresplsql

提问by Virat Kadaru

I have pairs of tablesin the format TABLEand TABLE_TWINnow

我有成对的表格格式TABLETABLE_TWIN现在

  • TABLEis the main table with lots of data
  • TABLE_TWINis a table with the exact same fields with a little data (different data)
  • TABLE是包含大量数据的主表
  • TABLE_TWIN是具有完全相同字段和少量数据(不同数据)的表

Now I would like to copy all rows from TABLE_TWINto TABLEusing 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 INSERTstatements because these tables have around 50 attributes each.

现在我想使用存储过程将所有行复制TABLE_TWINTABLE。我有很多这样的表,并且可能希望存储过程将表名作为参数,以便我可以对每个表对使用相同的过程。我不想写INSERT语句,因为这些表每个都有大约 50 个属性。

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

我不擅长 PL/SQL,所以我需要一些帮助。

Thanks!

谢谢!

回答by Egor Rogov

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

SQL 并没有那么长......但是如果你更喜欢一个过程,这里是:

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;

回答by Klaus Byskov Pedersen

insert into table_twin (select * from table) 

should do it

应该做