开发者

Update using Join(s) - Multi DB/Table

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 = 12开发者_Python百科34

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


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?

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:

UPDATE table1 
   SET my_value = (
                   SELECT t2.tab1_id
                     FROM table2 AS t2 
                    WHERE t2.tab1_id = table1.id
                  )       
 WHERE id = 1234
       AND EXISTS (
                   SELECT * 
                     FROM table2 AS t2 
                    WHERE t2.tab1_id = table1.id
                  );

Note that while using the correlation name t1 for Ttble1 is valid syntax according to the SQL-92 Standard this will materialize a table and the UPDATE will 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 MERGE syntax, would may be able to use something close to this:

MERGE INTO table1 
   USING (
          SELECT t2.tab1_id
            FROM table2 AS t2
         ) AS source
      ON id = source.tab1_id
         AND id = 1234
WHEN MATCHED THEN
   UPDATE
      SET my_value = source.tab1_id;

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.

UPDATE table1
   SET my_value = 'foo'
 WHERE EXISTS (
               SELECT * 
                 FROM table2 AS t2 
                WHERE t2.tab1_id = table1.id
              );


on Oracle, you can update only one table , but you could think of using a trigger .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜