PLSQL 中Merge into和Update的用法

  这两天一直在处理关于SQL server存储过程转换到Oracle中,也发现一些oracle语句的区别。 

   在oracle 中有个语法:merge

   用法如下:

merge into 表1
  using 表2 或者(
select * from 表2)别名
  
on (表1.id=表2.id)
  
when matched then update set 表1.列=表2.列

   还有无条件的insert语句:

MERGE INTO products p  USING newproducts np
  
ON (1=0WHEN NOT MATCHED THEN
  
INSERT
  
VALUES (np.product_id, np.product_name, np.category)
  
WHERE np.category = 'BOOKS'

 

   update的写法:    

    今天改写一个T_sql语句到PL/SQL中,原句是通过两个表关联查询取出数据,然后更新,在oracle中是不支持这样的写法的。一开始通过不加条件直接写,发现全部语句被更新了。后来在where语句下增加id的限制条件。

update formfield s set typeid=nvl((select doctypeid from pipedoctype f where s.id=f.fieldid and rownum=1),'') where s.id in (select fieldid from pipedoctype);
commit;

 

   总结:   

     update语句区别:就是在更新的时候,其更新语句数据来源是从另一个关联表中取出时,使用上述方法;

posted @ 2008-10-29 22:24  zping  阅读(6650)  评论(0编辑  收藏  举报