zoukankan      html  css  js  c++  java
  • 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语句区别:就是在更新的时候,其更新语句数据来源是从另一个关联表中取出时,使用上述方法;

  • 相关阅读:
    Thinkphp3.2 PHPMailer 发送邮件
    13 代理模式
    12 状态模式
    11 组合模式
    10 迭代器模式
    9 模板方法模式
    8 外观模式
    MySQL Network--Localhost与127.0.0.1的差异
    MySQL Memory--内存分配相关参数
    mysqldump命令之single-transaction
  • 原文地址:https://www.cnblogs.com/zping/p/1322498.html
Copyright © 2011-2022 走看看