0

我有两个表,#Table1uniqueId,和列, cpty_id,cpty_code#Table2cpty_idcpty_codeunique_id

现在我想更新 #Table1 的 uniqueId

喜欢:

update #Table1 
set uniqueId = 
    (uniqeId from #Tabel2 
     where #Tabel2.cpty_id=#Table1.cpty_id 
       and #Table2.cpty_code=#Table1.cpty_code) 

如何通过更新选择查询在 oracle 中实现这一点(我想避免循环)

注意:它们只是两个表uniqueId的组合cpty_idcpty_code中的一个。

4

2 回答 2

2

使用JOIN

UPDATE 
  ( SELECT t1.uniqeId, 
           t2.uniqeId AS newUniqueId
    FROM 
        #Table1 t1
      JOIN 
        #Table2  t2
          ON  t2.cpty_id = t1.cpty_id 
          AND t2.cpty_code = t1.cpty_code
  ) tmp
SET 
    uniqueId = newUniqueId ;

或稍微更改您的代码(警告!当表 2 中没有关联行时,这会将NULL值放在 中的某些行中):table.uniqueId

update #Table1 
set uniqueId = 
    (SELECT #Table2.uniqeId from #Table2 
     where #Table2.cpty_id = #Table1.cpty_id 
       and #Table2.cpty_code = #Table1.cpty_code) ;
于 2013-01-07T07:25:41.687 回答
0

请尝试如下

update #Table1 
set uniqueId = #Table2.uniqeId 
    (select <column_list> from #Tabel2) table2 
     where #Tabel2.cpty_id=#Table1.cpty_id 
       and #Table2.cpty_code=#Table1.cpty_code
于 2013-01-07T07:26:51.287 回答