oracle merge into 的用法詳解+實例

          Posted on 2009-06-18 16:10 林光炎 閱讀(1906) 評論(0)  編輯  收藏 所屬分類: ORACLE
          oracle merge into 的用法詳解+實例

          作用:merge into 解決用B表跟新A表數據,如果A表中沒有,則把B表的數據插入A表;

          語法:

          MERGE INTO [your table-name] [rename your table here]

          USING ( [write your query here] )[rename your query-sql and using just like a table]

          ON ([conditional expression here] AND [...]...)

          WHEN MATHED THEN [here you can execute some update sql or something else ]

          WHEN NOT MATHED THEN [execute something else here ! ]

          -------------------------------------實例-----------------------------------------------------------------

          merge into tfa_alarm_act_nms a
          using (select FP0,FP1,FP2,FP3,REDEFINE_SEVERITY
          from tfa_alarm_status) b
          on (a.fp0=b.fp0 and a.fp1=b.fp1 and a.fp2=b.fp2 and a.fp3=b.fp3)
          when matched then update set a.redefine_severity=b.redefine_severity
          when not matched then insert (a.fp0,a.fp1,a.fp2,a.fp3,a.org_severity,a.redefine_severity,a.event_time
          ,a.int_id)
          values (b.fp0,b.fp1,b.fp2,b.fp3,b.REDEFINE_SEVERITY,b.redefine_severity,sysdate,7777778);

          作用:利用表 tfa_alarm_status跟新表tfa_alarm_act_nms 的b.redefine_severity條件是a.fp0=b.fp0 and a.fp1=b.fp1 and a.fp2=b.fp2 and a.fp3=b.fp3,如果tfa_alarm_act_nms表中沒有該條件的數據就插入。

          如果你的數據量很大,此sql效率非常高。

          posts - 104, comments - 33, trackbacks - 0, articles - 0

          Copyright © 林光炎

          主站蜘蛛池模板: 柳江县| 河东区| 正蓝旗| 连州市| 塔河县| 禹城市| 佳木斯市| 南皮县| 仁布县| 黔江区| 衡东县| 苏尼特右旗| 锡林浩特市| 吉安县| 隆化县| 都安| 永丰县| 诸暨市| 永顺县| 阜新| 灵台县| 锡林郭勒盟| 三门峡市| 收藏| 子长县| 磴口县| 台南县| 皮山县| 大关县| 莎车县| 尚义县| 贵德县| 伊吾县| 枝江市| 谢通门县| 固镇县| 兴义市| 沈阳市| 金川县| 连南| 长治县|