Oracle : How to update multiple columns from different table? - sql

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'

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.

Error in SQL UPDATE statement

I want to update the fact table based on a lookup table. Fact table's Addressid needs to be updated and replaced by newaddress of lookup column and compared with oldaddress column of lookup table.
I have written following SQL statement:
Update [dbo].[fact_P]
Set Address_Id = (Select AddressID_new
From AddressLookup
Where fact_P.Address_Id = Lookup.AddressID_old)
but it's throwing an error.
You were referencing the AddressID_old column in the subquery using the wrong alias/table name. If I'm not mistaken, you should be using AddressLookup.Address_ID_old instead.
UPDATE [dbo].[fact_P]
SET Address_Id = (SELECT AddressID_new FROM AddressLookup al
WHERE fact_P.Address_Id = al.AddressID_old)
Use below query for UPDATE statement :
UPDATE [dbo].[fact_P] SET Address_Id = AddressID_new
FROM AddressLookup al
WHERE fact_P.Address_Id = al.AddressID_old
in where part, subquery is allowed only if it return 1 value. so if I am not mistake by your tables name as Lookup is 'AddressLookup', you need join:
Update fp
set fp.Address_Id=al.AddressID_new
from [dbo].[fact_P] fp join AddressLookup al
on fp.Address_Id= al.AddressID_old
Update tgt
set Address_Id = lu.Address_Id
from [dbo].[fact_P] tgt
inner join AddressLookup lu
on tgt.Address_Id = lu.AddressID_old;

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;

Update query whilst joining two tables?

I am trying to update a table whilst using a join. The task is:
Modify the database to show that Helen Partou has now learned to play the tambourine adequately.
Here is my attempt:
update MusicianInstrument
set instrumentName = 'Tambourine',levelOfExpertise = 'Adequate'
from MusicianInstrument join Musician
on MusicianInstrument.musicianNo = Musician.musicianNo
where musicianName = 'Helen Partou';
However I keep getting an error with the FROM statement.....any help?!
Thanks
You want to use WHERE EXISTS:
UPDATE MusicianInstrument mi
SET mi.instrumentName = 'Tambourine'
, mi.levelOfExpertise = 'Adequate'
WHERE EXISTS ( SELECT 1 FROM Musician m
WHERE m.musicianNo = mi.musicianNo
AND m.musicianName = 'Helen Partou');
On a side note since you're using Oracle I would recommend not using CamelCase or headlessCamelCase for object names.
Your task is to modify database, so probably not only update table but also insert values if they are not already there.
You can do this using merge:
merge into MusicianInstrument i
using (select * from Musician where musicianName = 'Helen Partou') m
on (m.musicianNo = i.musicianNo
and i.instrumentName = 'Tambourine')
when matched then update set levelOfExpertise = 'Adequate'
when not matched then insert (i.musicianNo, i.instrumentName, i.levelOfExpertise)
values (m.musicianNo, 'Tambourine', 'Adequate')

Multiple Query Writing in Single Query

I have two tables in Database , I need to select a field from one table and update it in another table with a condition where id is same .. Is it Possible to write in single query ???
This should work for you:
update storage
set storage.email = (select register.email
from register
where register.id = storage.id)
Yeah it is, you could do this for example:
UPDATE Origin SET DesiredColumn = NewValue
FROM Origin
JOIN NewTable ON Origin.Id = NewTable.Id
And guess the column names were like DesiredColumn in the updating table and NewValue in the table that holds the new value.
Yes, this is possible, although the syntax depends on the type of SQL you are using.
Here is an example for T-SQL (for Microsoft SQL Server)
UPDATE
S
SET
Email = R.Email
FROM
dbo.Register R
INNER JOIN dbo.Storage S
ON S.RegisterID = R.RegisterID