Subject Re: [firebird-support] Joins in an update query
Author Helen Borrie
At 09:39 AM 26/06/2003 +1000, you wrote:
>Can I do joins in an update statement?

No. You update tables, not output sets, and one update statement can
update only one table. You target the row(s) to update using a WHERE clause.


>I can't find examples in docs anywhere...
>
>e.g.
>update table1 t1
> left joint table2 t2
> on t1.int_key = t2.int_key
>set t1.test = 'something'
>where t1.int_key = 40;

update table1
set test = 'something'
where int_key = 40
and exists (select 1 from table2
where int_key = 40)

If you weren't targetting a particular value of int_key, but wanted to
update all rows that had a match in table2, you could use a correlated
exists() subquery, like

update table1 t1
set t1.test = 'something'
where exists (select 1 from table2 t2
where t2.int_key = t1.int_key)

or, if your application is selecting the value of int_key, you can target
the key using a parameter:

update table1 t1
set t1.test = 'something'
where t1.int_key = :param1
and exists (select 1 from table2 t2
where t2.int_key = t1.int_key)

heLen