有谁有示例数据迁移脚本(Oracle 10g 到 Oracle 10g,但架构不同)?

发布于 2024-08-14 08:24:52 字数 227 浏览 3 评论 0原文

我正在尝试编写一个数据迁移 pl/sql 脚本,以将一个模式中的某些数据传输到另一台服务器上的不同模式。第二个数据库最初是原始数据库的子集,但我们修改了架构。因此,我不能只对每个表使用以下内容:

Insert into DB2.table_name select * from DB1.table_name2; 

我尝试搜索示例脚本来显示如何执行此操作,但找不到任何内容。

I am trying to write a data migration pl/sql script to transfer some of the data in one schema to a different schema on another server. The second database started as a subset of the original database, but we have modified the schema. So I can't just use the following for each table:

Insert into DB2.table_name select * from DB1.table_name2; 

I have tried doing a search for sample scripts that show how to do this, but couldn't find anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空城旧梦 2024-08-21 08:24:52

如果 DB1.table_name 和 DB2.table_name 之间的列不同,那么您必须在插入语句中指定列列表。不幸的是,这里并没有真正的“灵丹妙药”。

话虽如此,为了加快该过程,您可以编写一些 PL/SQL 来生成插入语句,然后您可以手动修复这些语句。下面是执行此操作的示例 PL/SQL 代码。在此示例中,l_src_table 将是您的源表,l_target_table 将是您的目标表。显然,您仍然需要手动修复此代码生成的 SQL 语句,但这至少会生成一个模板 SQL,这将为您节省大量时间。

DECLARE
  l_insert_stmt VARCHAR2(4000);
  l_comma VARCHAR2(1) DEFAULT ' ';
  l_src_table VARCHAR2(500) := 'TABLE1';
  l_src_table_owner VARCHAR2(500) := 'DB1';
  l_target_table VARCHAR2(500) := 'TABLE2';
  l_target_table_owner VARCHAR2(500) := 'DB2';
BEGIN
  l_insert_stmt := 'INSERT INTO ' || l_target_table || ' ( ';
  FOR rec IN (SELECT column_name FROM all_tab_columns
              WHERE TABLE_name = l_target_table AND owner = l_target_table_owner)
  LOOP
     l_insert_stmt := l_insert_stmt || l_comma || rec.column_name;
     l_comma := ',';
  END LOOP;
  l_insert_stmt := l_insert_stmt || ' ) ';

  l_insert_stmt := l_insert_stmt || ' SELECT ';
  l_comma := ' ';
  FOR rec IN (SELECT column_name FROM all_tab_columns
              WHERE TABLE_name = l_src_table AND owner = l_src_table_owner)
  LOOP
     l_insert_stmt := l_insert_stmt || l_comma || rec.column_name;
     l_comma := ',';
  END LOOP;
  l_insert_stmt := l_insert_stmt || ' FROM ' || l_src_table;

  dbms_output.put_line(l_insert_stmt);
END;

If the columns are different between DB1.table_name and DB2.table_name then you're going to have to specify a column list in the insert statement. Unfortunately, there's not really a "magic bullet" here.

With that said, to speed up the process you could write some PL/SQL to generate the insert statements, and then you could fix those by hand. Here's a sample PL/SQL code to do this. In this example, l_src_table would be your source table and l_target_table would be your target table. Obviously, you'll still have to manually fix the SQL statement this code generates, but this will at least generate a template SQL which should save you a lot of time.

DECLARE
  l_insert_stmt VARCHAR2(4000);
  l_comma VARCHAR2(1) DEFAULT ' ';
  l_src_table VARCHAR2(500) := 'TABLE1';
  l_src_table_owner VARCHAR2(500) := 'DB1';
  l_target_table VARCHAR2(500) := 'TABLE2';
  l_target_table_owner VARCHAR2(500) := 'DB2';
BEGIN
  l_insert_stmt := 'INSERT INTO ' || l_target_table || ' ( ';
  FOR rec IN (SELECT column_name FROM all_tab_columns
              WHERE TABLE_name = l_target_table AND owner = l_target_table_owner)
  LOOP
     l_insert_stmt := l_insert_stmt || l_comma || rec.column_name;
     l_comma := ',';
  END LOOP;
  l_insert_stmt := l_insert_stmt || ' ) ';

  l_insert_stmt := l_insert_stmt || ' SELECT ';
  l_comma := ' ';
  FOR rec IN (SELECT column_name FROM all_tab_columns
              WHERE TABLE_name = l_src_table AND owner = l_src_table_owner)
  LOOP
     l_insert_stmt := l_insert_stmt || l_comma || rec.column_name;
     l_comma := ',';
  END LOOP;
  l_insert_stmt := l_insert_stmt || ' FROM ' || l_src_table;

  dbms_output.put_line(l_insert_stmt);
END;
叹倦 2024-08-21 08:24:52

您可以创建数据库链接

然后,如果您尝试从 db1 迁移到 db2:

Insert into table_name (f1, f2, etc) select (f1, f2, etc) from table_name2@DB2;

选择可以根据需要复杂或简单。

You can create a database link.

Then, if you're trying to migrate from db1 to db2:

Insert into table_name (f1, f2, etc) select (f1, f2, etc) from table_name2@DB2;

The select can be as complex or simple as needed.

勿忘心安 2024-08-21 08:24:52

如果您需要经常这样做,那么另一个选择是使用模式同步工具。 Toad、dbsolo 以及可能其他一些工具都可以使用。它节省了我很多时间和精力。

If you need to do this often enough,then another option is to use a schema synchronization tool. Toad, dbsolo and probably a few other tools can be used. It has saved me a lot of time and effort.

老街孤人 2024-08-21 08:24:52

可能有多种情况。当源表和目标表相同时,脚本如下所示。

INSERT INTO test2 (title, author)
SELECT title, author FROM test1;

但是,常见的情况是有多个源和一个目标。那么解决方案如下所示

INSERT INTO table ( col1 , col2, col3, col4)
SELECT p.col1, p.col2, c.col3, c.col4
FROM table1 p
INNER JOIN table2 c ON c.Id = p.Id

另外,检查合并查询以获得更好的结果更新时

There can be multiple cases. When source and destination table are same the script is like this

INSERT INTO test2 (title, author)
SELECT title, author FROM test1;

However, the common scenario is that there will be multiple sources and one destination. Then the solution is like below

INSERT INTO table ( col1 , col2, col3, col4)
SELECT p.col1, p.col2, c.col3, c.col4
FROM table1 p
INNER JOIN table2 c ON c.Id = p.Id

Also, check the Merge query for better results while updating

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文