2016-11-03 73 views
-2

我有這兩個表如下,我需要更新Table1.Active_flagY,其中Table2.Reprocess_FlagN更新oracle與加入兩個表

表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    | 
+--------+--------------+--------------+--------------+----------------+ 
+0

您的問題不可讀。 – FDavidov

+0

你有試過什麼嗎?發佈您的代碼,請 – Aleksej

+0

UPDATE hdfs_cntrl SET active_flag = 'Y' 其中源(來自proc_cntrl選擇源,其中Reprocess_Flag = 'N') 和subject_area中(選擇proc_cntrl subject_area其中Reprocess_Flag = 'N') 和source_table在(從proc_cntrl中選擇target_table,其中Reprocess_Flag ='N'); –

回答

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'); 
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'; 
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和插入和刪除,它可以做所有這三種類型的操作都在一個聲明中,它更加靈活,並且在某些情況下更高效(更快)。

0

UPDATE hdfs_cntrl SET active_flag = 'Y',其中源(來自proc_cntrl選擇源,其中Reprocess_Flag = 'N')和subject_area在(選擇proc_cntrl subject_area其中Reprocess_Flag = 'N'),並在source_table(選擇從proc_cntrl target_table Reprocess_Flag ='N')