merge into_目標表匹配列重複與源表匹配表重複的區別

wisdomone1發表於2012-12-12
SQL> select * from t_source;                                                                                            
                                                                                                                        
                                      A                                       B                                         
--------------------------------------- ---------------------------------------                                         
                                      1                                       1                                         
                                      2                                       2                                         
                                                                                                                        
SQL> select * from t_target;                                                                                            
                                                                                                                        
                                      A                                       B                                         
--------------------------------------- ---------------------------------------                                         
                                      2                                      34                                         
                                      2                                    3999                                         
                                                                                                                        
SQL> merge into t_target using t_source on (t_source.a=t_target.a) when matched then update set t_target.b=t_source.b;  
                                                                                                                        
Done                                                                                                                    
                                                                                                                        
SQL> select * from t_target;                                                                                            
                                                                                                                        
                                      A                                       B                                         
--------------------------------------- ---------------------------------------                                         
                                      2                                       2                                         
                                      2                                       2                                         
                                                                                                                        
SQL> commit;                                                                                                            
                                                                                                                        
Commit complete                                                                                                         
                                                                                                                        
小結:1,對於merge,目標表如果匹配列重複,但源表匹配不重複,會用源表表列新目標表的資料                                    
      2,反之,如果源表匹配列重複,此時merge就會報錯,因為不知用源表哪條記錄來更新目標表的資料                           

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9240380/viewspace-750968/,如需轉載,請註明出處,否則將追究法律責任。

相關文章