문제

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top