如何实时将一个oracle数据库table中选中的数据列插入到另一个oracle数据库table中

How to insert the selected data columns from one oracle database table into another oracle database table in real time

我有一个用例,我需要通过 运行 带有 where 子句的 select 语句来监视 oracle 数据库 XYZ 中的 table A。如果此 select 语句获取任何行,那么我需要立即将这些行插入另一个 table B,这是另一个 Oracle 数据库 MNO。

此外,假设当我在手表中并且第二次相同的 select 语句检索到已经在上面的步骤中插入的行但现在它有几列的值发生变化然后,立即other table B 也应该使用 table A.

中更新的列值进行更新

我可以使用 shell 脚本实现吗? 谁能告诉我如何实现这一点?

非常感谢您的提前建议。

我会说 merge 是你需要的:

merge into mno.table_2 b  -- or, possibly, table_2@mno
  using table_1 a
  on (a.id = b.id)
  when matched then update set
    (b.col1 = a.col1,
     b.col2 = a.col2
    )
  when not matched then insert (col1, col2)
    values (a.col1, a.col2);

注意第 1 行:

  • 如果 mno 确实是一个“数据库”(而不仅仅是同一数据库中的另一个用户),那么您必须使用数据库 link(第 1 行的注释部分)
  • 如果 mno 只是另一个用户,那么它应该 grant select, insert, update 在其 table 上给用户 xyz;否则,该操作将不起作用

Merge 也意味着您不必单独检查该行是否存在于另一个 table 中。


Shell 脚本?为什么?将所有内容保存在数据库中。

“守望”可以通过

完成
  • xyz.table_1 上创建一个数据库触发器,以便一旦发生变化,它就会触发并且 运行s merge
  • 或者,安排一个工作(使用 dbms_scheduler),它会定期(例如,每小时,一天两次,......)运行 merge