copying column from table - sql

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

Related

SQL merge statement with Joins

I am trying to use a merge statement for upsert operation. I want to do something like below where I join the both target and source table to the Student table for a unique key that only exists in the student table. Just to keep in mind, I cannot join StudentID. There could be a duplicate in my code. I need to join the schoolID for uniqueness.
Merge into GuardianTo gto inner join StudentTo sto on gto.fkStudentId = sto.StudentID
Using (Select * from GuardianFrom gfrom inner join StudentFrom sfrom on gfrom.fkStudentId = sfrom.StudentID) from
ON gto.guardianId = from.guardianId and sto.SchoolID = from.SchoolID
When....
I need to join the target guardianTo table with studentTo table to get the unique key of SchoolID. and match this ID with From tables. I know that this could be done in a separate insert and update statement (not merge), but is there a way to do something like the above using merge statement?
If you are using MSSQL you can do a merge like the code below. You need to supply a list of column names to use from your source query and then update/insert the columns you need in your target table (GuardianTo).
Merge into GuardianTo gto
Using (Select * from GuardianFrom gfrom inner join StudentFrom sfrom on gfrom.fkStudentId = sfrom.StudentID ) AS source (<column names in GuardianFrom, Studentfrom go here> )
ON (gto.guardianId = source.guardianId and gto.SchoolID = source.SchoolID)
WHEN MATCHED THEN
UPDATE SET <column in GuardianTo>= source.<column from source above> /* add any additional columns to update here*/
WHEN NOT MATCHED THEN
INSERT (guardianId, SchoolID, StudentID /* add any additional columns to insert here*/)
VALUES (source.guardianId, source.SchoolID, source.StudentID /* add any additional columns from the source to insert here*/);

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'

Need to use join in the where clause of a update statement in sql?

I need to join multiple tables in the where clause of a update statement. Precisely there are two tables with master slave relationship. I need to update a row in the master table but need to check for the foreign key entry in its slave table.
Table A
TableId,Empid,EmpName,EmpAdd
Table B
TableId,Empid,DeptId,DeptName
When a row is inserted in Table A, Table B also has an insert. Say I need to update EmpAdd of TableA and this shall be based on the columns Empid,DeptId,DeptName from the two tables. Therefore I guess I need to join two tables.
what about checking for EXISTS instead of JOINs:
UPDATE tbl_master m
SET m.some_column = some_value
WHERE m.masteID = updatetable_id
AND EXISTS (SELECT * FROM tbl_slave s WHERE s.masterID = m.masterID)

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
)