Error in SQL UPDATE statement - sql

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;

Related

Update column when a match in another table is found

I'm try to update a column in table B with a value in table A where the passport_no match.
Below is my sql query.
update tabel_b
set b.country_id = a.national_id
from table_a a
join tabel_b b on b.passport_no = a.passport_no
where a.is_deleted = false;
On executing, I get ERROR: column "b" of relation "tabel_b" does not exist
How can I go about this please.
The immediate error is that you can't qualify the column you want to update. So it should be set country_id = ...
The bigger problem is however your join.
As documented in the manual
Do not repeat the target table as a from_item unless you intend a self-join
So the correct statement most probably should be:
update tabel_b
set country_id = a.national_id
from table_a a
where b.passport_no = a.passport_no
and a.is_deleted = false;

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 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')

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

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