Dilip Dilip - 11 months ago 36
SQL Question

Need to update data from another database using db link

I have a table A with null dates (

) in BI database. I need to update those nulls with the right dates from AFLDEV DB using a DB link
. Common key is
in AFLDEV and
in BI DB. I have framed the following query but it does not work:

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.integration_id in (SELECT T.integration_id
FROM (SELECT * FROM w_product_d ORDER BY w_product_d.integration_id )T

If I run this query it updates all the dates to nulls but I need the opposite to happen i.e. replace null with the right dates.

Please help me out with this! I am doing this on SQL Developer for Oracle DB.

Answer Source

I think you've gotten all tied up between the rows you're updating and the rows you're using to update the column values with.

If you think about it, you're wanting to update rows in your w_product_d table where the created_on_dt is null, which means that your update statement will have a basic structure of:

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

Once you have that, it's easy then to slot in the column you're updating and what you're updating it with:

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;