I have a scenario where I would like to update multiple fields in multiple Tables using just one instuction. I need a Syntax to perform such opperations on multiple Databases (Oracle and MSSQL).
At the moment I am stuck at the following statement from MSSQL:
update table1
set table1.value = 'foo'
from table1 t1 join table2 t2 on t1.id = t2.tab1_id
where t1.id = 1234
I would like to update a field in t2 aswell in the same statement.
Further I would like to perform the same Update(s) on Oracle.
EDIT:
Seems like I can not update multiple Tables in just one statement.
Is there a syntax that works for Oracle and MSSql when updating using a Join?
Regards
I assume when you re-posed the question you want syntax that will work on both Oracle and SQL Server even though it will inevitably affect only one table.
Entry level SQL-92 Standard code is supported by both platforms, therefore the following ‘scalar subqueries’ SQL-92 code should work:
Note that while using the correlation name
t1forTtble1is valid syntax according to the SQL-92 Standard this will materialize a table and theUPDATEwill then target the materialized table ‘t1’ and leave your base table ‘table1` unaffected, which I assume is not the desired affect. While I’m fairly sure both Oracle and SQL Server are non-compliant is this regard and that in practise would work as expected, there’s no harm in being ultra cautious and sticking to the SQL-92 syntax by fully qualifying the target table.Folk tend not to like the ‘repeated’ code in the above subqueries (even though the optimizer should be smart enough to evaluate it only once).
More recent versions of Oracle and SQL Server support both support Standard SQL:2003
MERGEsyntax, would may be able to use something close to this:I just noticed your example is even simpler than I first thought and merely requires a simple subquery that should run on most SQL products e.g.