How can I update multiple rows in one query? - sql

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.

Related

Is there a better way to write update table1 where x in (select x from table2 inner join table1)?

I have an sql update statement:
update table1 set col1='val' where id in (select t2.id
from table2 t2
inner join table1 t1 on t1.id = t2.id
where t1.col2='val2' and t2.col3='val3');
Is there a better way to write it? I am thinking somehow not to use join since I have the table in the update construction.
Maybe:
update table1 set col1='val' where id in (select t2.id
from table2 t2
where t2.col3='val3')
and t1.col2='val2';
Is it better the last query?
Maybe with exists() ?
UPDATE table1 s
SET s.col1='val'
WHERE EXISTS(SELECT 1 FROM table2 t
WHERE s.id = t.id AND someCondition)

How to update multiple columns in the oracle SQL on ATP fusion DB

I am trying to use the below query, its everytime saying cannot insert null for the second column.
UPDATE TABLE1
SET (COL1,COL2,COL3) = (SELECT COL1,COL2,COL3
FROM TABLE2
WHERE t1.id = t2.id);
Try this:
UPDATE TABLE1 SET (COL1,COL2,COL3) =
(SELECT COL1,NVL(COL2,0),COL3 FROM TABLE1 T1, TABLE2 T2 where t1.id = t2.id)
WHERE <YOUR WHERE CONDITION>
I would use a MERGE:
MERGE INTO TABLE1 t1
USING (SELECT id, COL1,COL2,COL3
FROM TABLE2) t2
ON (t1.id = t2.id)
WHEN MATCHED THEN UPDATE SET t1.COL1 = t2.COL1
, t1.COL2 = t2.COL2
, t1.COL3 = t2.COL3;
There are rows in table1 where not corresponding row exists in table2 thus the sub-query returns nothing which means the UPDATE statement will take that as null for all three columns.
You have to add a WHERE clause that only updates rows in table1 that do have a matching row in table2
UPDATE TABLE1
SET (COL1,COL2,COL3) = (SELECT COL1,COL2,COL3
FROM TABLE2 t2
WHERE table1.id = t2.id)
where exist (select *
from table2 t2
where table1.id = t2.id);

Oracle SQL correlated update low performance

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

Using subquery in an update always requires subquery in a where clause?

This is a common SQL query for me:
update table1 set col1 = (select col1 from table2 where table1.ID = table2.ID)
where exists (select 1 from table2 where table1.ID = table2.ID)
Is there any way to avoid having two nearly identical subqueries? This query is an obvious simplification but performance suffers and query is needlessly messy to read.
Unfortunately Informix don't support the FROM clause at UPDATE statement.
The way to workaround and you will get better results (performance) is change the UPDATE to MERGE statement.
This will work only if your database is version 11.50 or above
MERGE INTO table1 as t1
USING table2 as t2
ON t1.ID = t2.ID
WHEN MATCHED THEN UPDATE set (t1.col1, t1.col2) = (t2.col1, t2.col2);
Check IBM Informix manual for more information
Update with inner join can be used to avoid subqueries
something like this:
update t1
set col1 = t2.col1
from table1 t1
inner join table2 t2
on t1.ID = t2.ID
try this:
update table1 set col1 = (select col1 as newcol from table2 where table1.ID = table2.ID)
where exists (newcol)

Using an inner join with subqueries in an update syntax

I am trying to use a inner join with an update statement with a subquery ... can you help me out with the sytax please --- and also how do you use the AS clause for alias in sql server???
the following is what i am trying to do :
Update Table1
inner join table2
set table1.value1 = (select table2.value1 where table1.value 1 ....)
any idea??
If you need to use a subquery to perform the UPDATE you can do it this way:
UPDATE t1
SET t1.value = t2.value
FROM Table1 t1
JOIN
(
SELECT id, value
FROM table2
) t2
ON t1.id = t2.id
One way is to alias the table:
update t1
set table1.value1 = t2.value1
from table1 as t1
join table2 as t2
on t1.id = t2.t1_id
You should try
UPDATE table1 SET t1.value1 = t2.value2
FROM table1 t1
INNER JOIN table2 t2
ON t1.field1 = t2.field2
UPDATE Table1 t1
INNER JOIN (
SELECT id, value
FROM table2
) t2 USING(id)
SET t1.value = t2.value