SQL更新以部分同步两个不同表中的两列

时间:2015-01-22 21:59:24

标签: sql db2

我有两张桌子:

Table 1: S490JR
USER         ROCOMP ROSUPC  REFID          QREADSTAT                 
MARQUEE  AQUA     E31       4081200201      1                        
MARQUEE  AQUA     E31       4102700301      1                        
TENNYSON AQUA     E34       4102101701      0                        
TENNYSON AQUA     E34       4102704501      0                        


    and table2: AB27PR                                         

USER      XCCOMP    XCSUPN  XEFID        XREADSTAT                   
MARQUEE AQUA      E31       4081200201  0                          
MARQUEE AQUA      E31       4081200201  0                          
MARQUEE GBRT      A183    50129004    0                          
MARQUEE GBRT      A205    50126025    0  

如果S490JR.USER与AB27PR.USER匹配,ROCOMP与XCCOMP匹配,REFID与XEFID匹配,那么只有当XREADSTAT为0且QREADSTAT为1时,我才想用QREADSTAT更新XREADSTAT值。

1 个答案:

答案 0 :(得分:1)

您可以使用MERGE

merge into AB27PR AB
    using (select USER, ROCOMP,REFID
             from S490JR
            where QREADSTAT =1) SR
    on
       (AB.USER= SR.ORDER_ID
        AND AB.XCCOMP = SR.ROCOMP
        AND AB.XEFID = SR.REFID )
    when matched 
     and AB.XREADSTAT = 1
    then update set XREADSTAT = SR.QREADSTAT;