I know its almost there, but base is telling me it can't find a column called table1.id when I know its there!
UPDATE table2 SET col1 = (SELECT field1 FROM table1 WHERE table2.id = table1.id) WHERE table1.id = table2.id
UPDATE table2 SET col1 = (SELECT field1 FROM table1 WHERE table2.id = table1.id)
table1 is unknown in the outer SQL.
What I get from your query, this will work
UPDATE table2 SET col1 = t1.field1
FROM table2 t2 INNER JOIN table1 t1 ON t2.id = t1.id
Instead of using a WHERE clause, try using an INNER JOIN clause. It is indeed late so forgive me for my code haha
UPDATE table2
SET col1 = (SELECT field1
FROM table1
WHERE table2.id = table1.id)
INNER JOIN table1
ON table2.id = table1.id
Option 1: No need to have outer WHERE clause.
Option 2: Do not use inner query unneccesarily. Use Inner Join instead
Related
I've go this request in SQL for a migration:
UPDATE table1
SET table1.value = table2.value
FROM table1
INNER JOIN table2
ON table2.othervalue2 = table1.key
WHERE table2.othervalue3 is NULL
Works well.
But not with Oracle. I know it isn't impossible to make join in a update with oracle, so I use this:
UPDATE table1 SET table1.value = (SELECT table2.value
FROM table2
WHERE table2.othervalue2 = table1.key
AND table2.othervalue3 is NULL)
WHERE EXISTS (SELECT table2.value
FROM table2
WHERE table2.othervalue2 = table1.key
AND table2.othervalue3 is NULL)
but I have an ORA-01427: Subquery returns more than one row.
Thanks
i would say you have inconsistent data. From the Errors you get i would say you have duplicates in the query specified.
SELECT table2.value
FROM table2
WHERE table2.othervalue2 = table1.key
AND table2.othervalue3 is NULL
you might wanne check it.or you need to have the data better specified you want to select for Update/Merge.
I need to bring the below sample code from mssql to Teradata. Please let me know how to convert it.Sample code -
Update table1
set table1.name = table3.name
from table1
inner join table2
on table2.id = table1.id
left join table3
on table2.id = table3.id where table3.name is null
It's ugly, but this should work. You can get around Teradata not allowing outer joins in an update by using a derived table.
update table1
from table1,
(select <column list> from table2 left join table3 on table2.id = table3.id) t
set ...
where
table1.id = t.id
and t.name is null
Like the others mentioned, you should check your NULL condition. Nevertheless, here's one more option:
Update table1
FROM (
select table1.id, table3.name
from table1
inner join table2
on table2.id = table1.id
left join table3
on table2.id = table3.id where table3.name is null
) src
SET table1.name = src.name
WHERE table1.id = src.id
You just move your original source query into a "src" sub-query and update from it. Just make sure in your "src" query you only have one row for each table1.id value so you don't get target row updated by multiple source rows errors.
I think your logic is better handled by not exists:
Update table1
set table1.name = null
where not exists (select 1
from table2 join
table3
on table2.id = table3.id
where table2.id = table1.id
);
This is not exactly what your query specifies -- this will update table1.name when there is no match in table2. If that is an issue, you can do:
update table1
set table1.name = null
where exists (select 1
from table2
where table2.id = table3.id
) and
not exists (select 1
from table2 join
table3
on table2.id = table3.id
where table2.id = table1.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.
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)
Let's say there are 2 tables Table1 { ID, Name, Other } and Table2 { ID, Name, Other }. Both of them have identical records with the same IDs except that in Table1 all Name values are NULL. How can I import Name values from Table2 to Table1 using T-SQL (SQL Server 2008)?
Update Table1
Set Table1.Name = Table2.Name
From
Table1 INNER JOIN Table2 on Table1.ID = Table2.ID
You're looking for the MERGE command, which is like the UPSERT that you've probably read about elsewhere. Here's a quick article about it.
UPDATE Table1
SET Table1.Name = Table2.Name
FROM Table2
WHERE Table1.Id = Table2.Id
--AND Table1.Name IS NULL
Just join the tables and update:
update t1
set [Name] = t2.Name
from Table1 t1
inner join Table2 t2 on t2.ID = t1.ID