oracle 更新一个表的字段到更一个表里两种方法

方法1. 结合具体示例如下

UPDATE T_REPORT_MONTHLOGASLEVEL T
   SET T.KEY1 =
       (SELECT P.STRU_ORDER
          FROM PUB_STRU P
         WHERE P.STRU_LEVEL = 2
           AND T.LEVEL_2_COMPANY = P.ORGAN_ALIAS)

方法2.结合具体示例如下

MERGE INTO T_REPORT_MONTHLOGASLEVEL T
USING (SELECT P.STRU_ORDER, P.ORGAN_ALIAS
         FROM PUB_STRU P
        WHERE P.STRU_LEVEL = 2) NP
ON (NP.ORGAN_ALIAS = T.LEVEL_2_COMPANY)
WHEN MATCHED THEN
  UPDATE SET T.KEY1 = NP.STRU_ORDER

oracle9i以后新增加的merge into 方法 方法2性能远远优于方法1

 

merge into 使用方法如下(摘自网上)

语法如下 
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的用法 
merge into products p using newproducts np on (p.product_id = np.product_id) 
when matched then 
update set p.product_name = np.product_name 
when not matched then 
insert values(np.product_id, np.product_name, np.category) 

 

你可能感兴趣的:(oracle)