Update field in a table on a database 1 from an external identical database 2 - sql

To keep this shot.
I would like to update a field of product_price with all prices where I keep on an external demo table.
I tried to use the following query but it's throwing error
UPDATE dest
SET product_price = src.product_price
FROM DB2.trades AS dest
INNER JOIN DB1.trades AS src
ON dest.KEY = src.KEY
--And KEY = '12323';
Could you please help with how to do this?

Presuming that table you're updating resides in database DB2 and "external" table resides in another database (DB1), you'll need to create a database link to DB1 (let's call it dbl_db1) and reference its table as trades#dbl_db1. Something like this:
update trades d set
d.product_price = (select s.product_price
from trades#dbl_db1 s
where s.key = d.key
)
where exists (select null from trades#dbl_db1 a
where a.key = d.key
);
Or, using MERGE:
merge into trades d
using trades#dbl_db1 s
on (s.key = d.key)
when matched then update set
d.product_price = s.product_price;

Related

Update a flag only if ALL matching condition fails

I am trying to write a query in oracle to only update a flag based on below scenario :
Scenario :
A mctn_id is linked with multiple PRPR_ID and each PRPR_ID can have different addresses, I need to update flag as N in a table if ALL PRPR_ID addresses don't belong to config table address. If any of it belongs to config table address then it shouldn't update the flag as N.
I am using not exists in this case which is not working.
update prcb_enroll_tbl
set prov_flg ='N',
sys_insert_dtm = systimestamp
where tin_number in (select mctn_id
from cc_pr_prov prpr
inner join cc_pr_addr prad
on prpr.prpr_id = prad.prad_id
and not exists (select 1
from fsg_prcb_config config
where prad.prad_addr1 = config.config_value)
The above query is updating a flag even if only one of the addresses belongs to config table which is not the expected outcome.
This shoulds like not exists. Does this do what you want?
update prcb_enroll_tbl pe
set prov_flg ='N', sys_insert_dtm = systimestamp
where not exists (
select 1
from cc_pr_prov pr
inner join cc_pr_addr pa on pr.prpr_id = pz.prad_id
inner join fsg_prcb_config pc on pc.config_value = pa.prad_addr1
where ??.mctn_id = pe.tin_number
)
It is unclear which table column mctn_id comes from, so I used ???: you should replace it with the correct table alias.

Oracle : How to update multiple columns from different table?

I am using oracle database and have a situations to update fields from some other tables. My issue is it is updating all the records instead of specified conditions.
For example, I am trying to update perm_address and temp_address in EMPLOYEE table from ADDRESS table. Right now, I am using below query. But, it is updating all the records.
UPDATE EMPLOYEE EMP
SET (EMP.PERM_ADDRESS, EMP.TEMP_ADDRESS) =
(SELECT ADDR.PERM_ADDR,ADDR.TEMP_ADDR
FROM ADDRESS ADDR
WHERE ADDR.ID=EMP.ADDRESS_ID
);
In Oracle how to handle this situations? Normally, how to handle the update from multiple table into source table?
Thanks in advance....
Add a WHERE clause to update only matching records:
UPDATE EMPLOYEE EMP
SET (EMP.PERM_ADDRESS, EMP.TEMP_ADDRESS) =
(SELECT ADDR.PERM_ADDR, ADDR.TEMP_ADDR
FROM ADDRESS ADDR
WHERE ADDR.ID = EMP.ADDRESS_ID
)
WHERE EXISTS (SELECT 1 FROM ADDRESS ADDR WHERE ADDR.ID = EMP.ADDRESS_ID);
Updating a table with data from another table is often simpler using the MERGE statement. https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9016.htm
Something like this:
merge into employee emp
using address addr
on (addr.id = emp.address_id)
when matched
then update
set emp.perm_address = addr.perm_addr,
emp.temp_address = addr.temp_addr;
updating one table by another table - the basic format is
--ORACLE
update tableX t set (t.fldA, t.fldB) =
(select fldA, fldB from table_B where ID ='X')
where t.ID = 'Y'

Update one table data from another table

I have source table and target table.
both source table and target table contains same columns id,name,age,time
i am updating source table by target table
so for this process i have tried this query.but it does't work.can you please tell me what is wrong in this query?
UPDATE source
SET source.name = target.name,
source.age = target.age,
source.time = target.time
FROM target
INNER JOIN source
ON source.id = target.id;
What I have heard is Oracle throws this error when updating a table by joining to another table. Try using an Subquery something like this
UPDATE source a
SET a.name = (
select b.SURNAME
from target b
where a.id = b.id
)

Update database from another using joins?

I am trying to update a table from another database using joins and having a hard time. This is what I am trying to do in pseudo:
UPDATE [Database1].[dbo].[Sessions]
SET [SpeakerID] = ?STATEMENT1?
WHERE ?STATEMENT2?
For "Statement1", this would be coming from another database and table that has columns: SessionID and SpeakerID. How can this be achieved?
UPDATE a
SET a.SpeakerID = b.colName -- SET valoue here
FROM Database1.dbo.Sessions a
INNER JOIN Database2.dbo.Sessions b
ON a.SessionID = b.SessionID -- assumes that their
-- relationship column is SessionID,
-- change it in your original columnName
WHERE ....
a and b are called alias. They are useful when you have longer source name.
UPDATE L
SET SpeakerID = R.SpeakerID
FROM dbo.LocalTable AS L
INNER JOIN RemoteDatabase.dbo.RemoteTable AS R
ON L.SomeValue = R.SomeValue;
This really is no different from this problem except you have to add a database prefix to one of the tables in the join.
try
UPDATE [Database1].[dbo].[Sessions]
SET
Sessions.col1 = other_table.col1
FROM
[Database1].[dbo].[Sessions] Sessions
INNER JOIN
[Database2].[dbo].other_table AS other_table
ON
Sessions.id = other_table.id
WHERE Sessions.id = ??
Note if the database is on another server you will need to create a linked server first
http://msdn.microsoft.com/en-us/library/ff772782.aspx

Update statement involving two different catalogs

Trying to execute an update of a single field where the current db wants to pull values from last night's backup. Should be something close to:
update myTable
set status = (select status
from .lastBackup.dbo.myTable as t
where t.idField = idField)
Help?
Try this:
update t
set status = b.status
from myTable t
inner join lastBackup.dbo.myTable b
on t.idField = b.idField