如何写一个sql语句可以根据table的主键去判断是否insert还是update,即如果主键已经存在了就update,否则insert,最好是通用的sql,谢谢啦~~~

解决方案 »

  1.   

    merge  into  a  using  b  on  (a.USER_NO=b.USER_NO) 
      when  matched  then  update  set  a.NAME=b.NAME 
      when  not  matched  then    insert  values  (b.USER_NO,b.NAME) 
      

  2.   

      --用merge
      Merge  into ta a
      using tb b
      on (a.id = b.id)
      when matched then update set a.name=a.name
      when not matched then insert(a.id,a.name)
      values(b.id,b.name);
      

  3.   

    我需要写一个程序,程序中用到sql,而值是未定的,例如insert into A valuse(?,?);update A set id=?,name=?.这个应该怎么写呢?谢谢!
      

  4.   

    1楼速度好快假设你要插入的记录是(user_no,name) '1','Jack'
    将b改下可以写成(select '1' user_no, 'Jack' name from dual) b 
      

  5.   

    merge into a
    using (select 5 t1 from dual) b
    on (a.t1= b.t1)
    when matched then
        update set a.t2= 'aaaa'
    when not matched then
        insert values ('test', b.t1)
    需要注意的是 ,a.t1是条件,所以不能更新他ziduan
      

  6.   

    merge  into  a 
    using  (select ? user_no, ? name from dual) b  
    on  (a.USER_NO=b.USER_NO) 
    when  matched  then  
    update  set  a.NAME=b.NAME 
    when  not matched then    
    insert  values  (b.USER_NO,b.NAME) ;这样不可以吗?
      

  7.   


    你是正确的,thank you~!~~