I want to update a record for a table and based on foreign key from other table..
Update table1 t1
Inner join table2 t2 on t2.id = t1. Id
Set t1. Name ='abc'
Where t2. User ='xyz';
That syntax is not valid in Oracle use MERGE
MERGE INTO table2 trg
using table1 src
ON (trg.id = src.id
AND trg.user = 'xyz')
WHEN matched THEN
UPDATE SET trg.NAME = 'abc';
You can also use Exists
Update table1
Set Name ='abc'
Where exists (select 1
from table2 t2
where t2.id = table1.Id
and t2.User ='xyz');
You can use this query
MERGE INTO table1 t1
USING table2 t2
ON (t1.id = t2.id and t2.User='xyz')
WHEN MATCHED THEN
update
set t1.Name = 'abc';
Related
I have table1 and table2. They have the same columns and the column ID is the one that i can use to connect the tables.
How can i run foreach statment that will update row Name in table1 with the value for column Name in table2?
I need this so i can fix the column Name in Table1 because it is incorect , and the good values for it are in table2
I tried using a single update statement but it takes forever to execute because both tables are with over 600 000 rows
update
table1 t1
set
(
t1.name
) = (
select
t2.name
from
table2 t2
where
t2.id = t1.id
and
rownum = 1
)
where exists (
select
null
from
table2 t2
where
t2.id = t1.id
);
For this query:
update table1 t1
set t1.name = (select t2.name from table2 t2 where t2.id = t1.id and rownum = 1)
where exists (select 1
from table2 t2
where t2.id = t1.id
);
You want an index on table2(id, name).
A simple inner join should work this.
UPDATE T1
SET T1.NAME = T2.NAME
FROM MyTable T1
INNER JOIN MyOtherTable T2
ON T1.ID = T2.ID
I want to select t2 column values (Name) and update with t1 column values (Name) without where clause. what will be the query?
I am executing below code:
update t1 set t1.name=t2.name from t2 where t1.id=t2.id
but I want to perform it without WHERE clause.
Join both tables on id:
UPDATE t1 SET t1.Name = t2.Name
FROM TableName1 t1
INNER JOIN TableName2 t2 ON t1.Id = t2.Id
We use MERGE nowadays:
MERGE t1 USING t2 ON t1.id = t2.id
WHEN MATCHED THEN
UPDATE SET name = t2.name;
just do it
UPDATE t1
SET t1.name=t2.name
FROM table1 t1 INNER JOIN table2 t2 ON t1.id=t2.id
How can I update multiple rows in one query?
I have something like this:
update POL_VYMFOND set fk_vsoub='2245'
where fk_vsoub in (select HL_VYMSOUB.ID_VSOUB
from POL_VYMSEZN
inner join HL_VYMSEZN
on HL_VYMSEZN.ID_VSEZN=POL_VYMSEZN.FK_VSEZN
inner join HL_VYMSOUB
on HL_VYMSOUB.FK_VSEZN=HL_VYMSEZN.ID_VSEZN
where POL_VYMSEZN.FK_BUDOVA='4')
but definitely wrong.
Is it possible to do this?
I would like to change column values in one table according to values from another table.
Thank you
Generic answer for future developers.
SQL Server
UPDATE
t1
SET
t1.column = t2.column
FROM
Table1 t1
INNER JOIN Table2 t2
ON t1.id = t2.id;
Oracle (and SQL Server)
UPDATE
t1
SET
t1.colmun = t2.column
FROM
Table1 t1,
Table2 t2
WHERE
t1.ID = t2.ID;
MySQL
UPDATE
Table1 t1,
Table2 t2
SET
t1.column = t2.column
WHERE
t1.ID = t2.ID;
Firebird 2.1
UPDATE dest_table t1
SET
field1 = (select field1 from src_table t2 where t2.pk = t1.pk),
field2 = (select field2 from src_table t2 where t2.pk = t1.pk)
WHERE EXISTS (select 1 from src_table t2 where t2.pk = t1.pk)
For other versions of Firebird please check this link
Hope this will help you, and will solve your issue.
I want to update a column of table based on a condition. It should check if the value exists in other table, if exists then value from other table will be used else value from same table will be used.
Update table1
Set column1=(select t2.alias||’#email.com’ as new_name
From table2 t2, table1 t1, table3 t3
Where t1.id=t2.id
And t1.id=t3.id
Else if
Select t2.alias is null or t2.id is null
then column1= select t1.id||’#email.com’ as new_name
Any suggestions on this??
Thanks in advance.
Does this do what you want?
Update table1
Set column1 = (select (case when t2.alias is not null and t2.id is not null then t2.alias
else t1.id
end) ||'#email.com' as new_name
From table1 t1 left outer join
table2 t2
on t1.id=t2.id
);
I removed table3 because it does not seem to be used. With left join is won't even be filtering any results.
By leaving out the "insert" half of the merge statement, you can make it into a strictly update statement:
MERGE INTO table1 t
USING(
SELECT t2.id, t2.Alias
FROM table2 t2
JOIN table1 t1
ON t1.id = t2.id
AND t1.Alias <> t2.Alias
) tu
on( tu.id = t.id )
WHEN MATCHED THEN
update set t.Alias = tu.Alias;
The query will return only those existing values from table2 that differ from table1. To make it exactly match your requirement (update table1 if any value exists in table2) then remove the line AND t1.Alias <> t2.Alias, but why update a field to the same value it already has?
I have to update table(table3) with data from two other tables.
These two tables (table1,table2) are both describing single object, therefore table1.id equals table2.id for that single object.
Table 3 is a many-to-many mapping table for that object and another one which is not important here. Into that mapping table I need to put some data from table1 and table2.
I am trying to update table3 with correlated query, but it takes a very long time to complete. I ran this query on group of ~500.000 records and it runs for over an hour now.
Am I doing something wrong here?
UPDATE table3 t3
SET (t3.some_value1, t3.other_value1, t3.some_value2, t3.other_value2) =
( SELECT t1.some_value1, t1.other_value1, t2.some_value2, t2.other_value2
FROM table1 t1 JOIN table2 t2 ON t1.id = t2.id
WHERE t3.fk = t1.id)
WHERE EXISTS (SELECT 1 FROM table1 t1 JOIN table2 t2 ON t1.id = t2.id WHERE t3.fk = t1.id);
SELECT * FROM table3;
SQL Fiddle
Use this:
UPDATE
(SELECT
T3.SOME_VALUE1 TARGET1,
T3.OTHER_VALUE1 TARGET2,
T3.SOME_VALUE2 TARGET3,
T3.OTHER_VALUE2 TARGET4,
T1.SOME_VALUE1 SOURCE1,
T1.OTHER_VALUE1 SOURCE2,
T2.SOME_VALUE2 SOURCE3,
T2.OTHER_VALUE2 SOURCE4
FROM
TABLE1 T1
JOIN TABLE2 T2
ON T1.ID = T2.ID
JOIN T3
ON T3.FK = T1.ID)
SET
TARGET1 = SOURCE1,
TARGET2 = SOURCE2,
TARGET3 = SOURCE3,
TARGET4 = SOURCE4;
If you face ORA-01779 cannot modify a column which maps to a non key-preserved table then try using UPDATE /*+ BYPASS_UJCV */ or add a unique index on T1.ID, T2.ID, T3.FK
The answer provided by #SriniV uses a technique called an Updatable Join View. In later versions of Oracle, this can be replaced with a MERGE:
MERGE INTO t3
USING (
SELECT t1.id
t1.some_value1,
t1.other_value1,
t2.some_value2,
t2.other_value2
FROM table1 t1
JOIN table2 t2 ON t1.id = t2.id
WHERE 1=1
AND t3.fk = t1.id
) a ON (
t3.fk = a.id
)
WHEN MATCHED THEN UPDATE
SET t3.some_value = a.some_value,
t3.other_value1 = a.other_value1
t3.some_value2 = a.some_value2
t3.other_value2 = a.other_value2