加入两个表在oracle中更新

时间:2016-11-03 12:19:39

标签: oracle oracle11g

我在下面有这两个表格,我需要将Table1.Active_flag更新为Y,其中Table2.Reprocess_FlagN

表1

+--------+--------------+--------------+--------------+-------------+
| Source | Subject_area | Source_table | Target_table | Active_flag |
+--------+--------------+--------------+--------------+-------------+
| a      | CUSTOMER     | ADS_SALES    | ADS_SALES    | N           |
| b      | CUSTOMER     | ADS_PROD     | ADS_PROD     | N           |
| CDW    | SALES        | CD_SALES     | CD_SALES     | N           |
| c      | PRODUCT      | PD_PRODUCT   | PD_PRODUCT   | N           |
| d      | PRODUCT      | PD_PD1       | PD_PD1       | N           |
| e      | ad           | IR_PLNK      | IR_PLNK      | N           |
+--------+--------------+--------------+--------------+-------------+  

表2

| Source | Subject_area | Source_table | Target_table | Reprocess_Flag |
+--------+--------------+--------------+--------------+----------------+
| a      | CUSTOMER     | ADS_SALES    | ADS_SALES    | N              |
| b      | CUSTOMER     | ADS_PROD     | ADS_PROD     | N              |
| CDW    | SALES        | CD_SALES     | CD_SALES     | N              |
| c      | PRODUCT      | PD_PRODUCT   | PD_PRODUCT   | Y              |
| d      | PRODUCT      | PD_PD1       | PD_PD1       | Y              |
| e      | ad           | IR_PLNK      | IR_PLNK      | N              |
+--------+--------------+--------------+--------------+----------------+

4 个答案:

答案 0 :(得分:1)

在一个select语句中使用所有三列。

UPDATE hdfs_cntrl SET active_flag = 'Y' 
where (source,subject_area ,source_table ) in  ( select source,subject_area ,source_table from proc_cntrl where Reprocess_Flag = 'N'); 

答案 1 :(得分:1)

基于另一个表中的数据更新一个表几乎总是最好用MERGE语句完成。

假设sourcetable2中的唯一键:

merge into table1 t1
  using table2 t2
  on (t1.source = t2.source)
when matched
  then update set t1.active_flag = 'Y'
  where t2.reprocess_flag = 'N'
;

如果您不熟悉MERGE语句,请阅读它 - 它与UPDATE和INSERT和DELETE一样容易学习,它可以在一个语句中完成所有三种类型的操作,它很多更灵活,在某些情况下,更有效(更快)。

答案 2 :(得分:0)

merge into table1 t1
using table2 t2
on (t1.sorce=t2.source and t1.Subject_area = t2.Subject_area and t1.Source_table = t2.Source_table and t1.Target_table = t2.Target_table and t2.flag_status = 'N')
when matched then update set
t1.flag = 'Y';

答案 3 :(得分:0)

UPDATE hdfs_cntrl SET active_flag ='Y'where source in(选择来自proc_cntrl的源,其中Reprocess_Flag ='N')和subject_area(从proc_cntrl选择subject_area,其中Reprocess_Flag ='N')和source_table in(从proc_cntrl中选择target_table,其中) Reprocess_Flag ='N')