Update one table data from another table - sql

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
)

Related

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

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;

Update CarValue column in destination based on source carValue

I have a destination table that needs to be updated with the values that match from my source. I would like my destination to have the same CarValue as my source table would have. I have came up with the query above to pull the records that do not match my source and I want to use this to update my destination table values. Please provide examples of how to accomplish this.
select
s.*
,t.*
from SourceTable as s
full outer join DestinationTable as t
on s.CarNo = t.CarNo and s.CarName = t.CarName
where s.CarValue <> t.Carvalue
You do not need a full join. So:
update t
set carvalue = s.carvalue
from DestinationTable t join
SourceTable s
on s.CarNo = t.CarNo and s.CarName = t.CarName
where s.CarValue <> t.Carvalue;
This does not update non-matching rows. My guess is that you want to leave those alone. If you do want to update them, use a left join and remove the where clause.
You can update by using the current join statement
update t
set t.CarValue = s.Carvalue
from DestinationTable as t
full outer join SourceTable as s
on s.CarNo = t.CarNo and s.CarName = t.CarName
where s.Carvalue<>coalesce(t.Carvalue,'xYz');
coalesce() is added for the probability of existence of null values within the DestinationTable, assuming a car value never would be xYz.
Demo

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'

MERGE vs. UPDATE

I was trying to look for it online but couldn't find anything that will settle my doubts.
I want to figure out which one is better to use, when and why?
I know MERGE is usually used for an upsert, but there are some cases that a normal update with with subquery has to select twice from the table(one from a where clause).
E.G.:
MERGE INTO TableA s
USING (SELECT sd.dwh_key,sd.serial_number from TableA#to_devstg sd
where sd.dwh_key = s.dwh_key and sd.serial_number <> s.serial_number) t
ON(s.dwh_key = t.dwh_key)
WHEN MATCHED UPDATE SET s.serial_number = t.serial_number
In my case, i have to update a table with about 200mil records in one enviorment, based on the same table from another enviorment where change has happen on serial_number field. As you can see, it select onces from this huge table.
On the other hand, I can use an UPDATE STATEMENT like this:
UPDATE TableA s
SET s.serial_number = (SELECT t.serial_number
FROM TableA#to_Other t
WHERE t.dwh_serial_key = s.dwh_serial_key)
WHERE EXISTS (SELECT 1
FROM TableA#To_Other t
WHERE t.dwh_serial_key = s.dwh_serial_key
AND t.serial_number <> s.serial_number)
As you can see, this select from the huge table twice now. So, my question is, what is better? why?.. which cases one will be better than the other..
Thanks in advance.
I would first try to load all necessary data from remote DB to the temporary table and then work with that temporary table.
create global temporary table tmp_stage (
dwh_key <your_dwh_key_type#to_devstg>,
serial_number <your_serial_number_type##to_devstg>
) on commit preserve rows;
insert into tmp_stage
select dwh_key, serial_number
from TableA#to_devstg sd
where sd.dwh_key = s.dwh_key;
/* index (PK on dwh_key) your temporary table if necessary ...*/
update (select
src.dwh_key src_key,
tgt.dwh_key tgt_key,
src.serial_number src_serial_number,
tgt.serial_number tgt_serial_number
from tmp_stage src
join TableA tgt
on src.dwh_key = tgt.dwh_key
)
set src_serial_number = tgt_serial_number;

copying column from table

I need to copy a column from one table to another. The problem is matching the values with the right rows.
INSERT INTO DestinationTable (money_owed)
SELECT "credit"
FROM SourceTable
How do I search through the destination table and compare another field to see if it's the same one in the source table?
You need to join the two tables on the matching condition.
Something like this
UPDATE
DestinationTable
SET
DestinationTable.money_owed = SourceTable.Credit
FROM
DestinationTable
INNER JOIN SourceTable
ON DestinationTable.Field2 = SourceTable.Field2
do an Update from:
UPDATE
destination
SET
destination.money_owed = source.Credit
FROM
destination
INNER JOIN
source
ON
source.id = destination.id