【问题标题】:Need to update data from another database using db link需要使用 db link 从另一个数据库更新数据
【发布时间】:2015-12-17 17:28:43
【问题描述】:

我在 BI 数据库中有一个包含空日期 (CREATED_ON_DT) 的表 A。我需要使用数据库链接mtl_system_items_b@afldev 使用 AFLDEV DB 中的正确日期更新这些空值。公共密钥是 AFLDEV 中的 inventory_item_id 和 BI DB 中的 integration_id。我已经构建了以下查询,但它不起作用:

UPDATE w_product_d
SET w_product_d.CREATED_ON_DT = (SELECT min(creation_date)  
FROM mtl_system_items_b@afldev B 
    where to_char(B.inventory_item_id)=w_product_d.integration_id
    and B.organization_id = '102'
    AND w_product_d.CREATED_ON_DT IS NULL
    and w_product_d.integration_id in (SELECT T.integration_id
FROM (SELECT * FROM w_product_d ORDER BY    w_product_d.integration_id )T
    WHERE T.CREATED_ON_DT IS NULL) 
);

如果我运行此查询,它会将所有日期更新为空值,但我需要相反的情况发生,即用正确的日期替换空值。

请帮我解决这个问题!我在 SQL Developer for Oracle DB 上执行此操作。

【问题讨论】:

  • 首先运行一个选择查询,这样您就可以看到发生了什么。然后将其转换为更新查询。

标签: sql database oracle join business-intelligence


【解决方案1】:

我认为您已经在要更新的行和用来更新列值的行之间纠结了。

如果您考虑一下,您希望更新 w_product_d 表中 created_on_dt 为空的行,这意味着您的更新语句将具有以下基本结构:

update w_product_d wpd
set    ...
where  wpd.created_on_dt is null;

一旦你有了它,就可以很容易地插入你正在更新的列以及你正在更新它的内容:

update w_product_d wpd
set    wpd.created_on_dt = (select min(creation_date)
                            from   mtl_system_items_b@afldev b
                            where  to_char(b.inventory_item_id) = wpd.integration_id)
where  wpd.created_on_dt is null;

【讨论】:

  • 感谢您的询问。现在很有意义。
  • 不客气!如果您发现自己在未来陷入了纠结,有时退一步将问题分解为各个步骤会有所帮助,然后从那里构建 sql 语句。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-16
  • 2018-06-17
  • 1970-01-01
相关资源
最近更新 更多