查询一张表的结果,在另一张表中没有name做添加有做修改操作!A表
name    fraction    status
张三      80          0 
李四      70          0
王五      58          1
B表
name    fraction
张三      20
王五      58
要个结果是
B表
name    fraction
张三      80          
李四      70          
王五      58 

解决方案 »

  1.   


    --更新
    update b
    set b.fraction = a.fraction
    from a join b on a.name = b.name--插入
    insert into b
    select name,fraction
    from a t
    where not exists (select 1 from b where name = t.name)
      

  2.   

    加个存储过程的创建,把那段放进去就是了。
    create proc get_UpdateAndInsert
    as
    begin
    --更新
    update b
    set b.fraction = a.fraction
    from a join b on a.name = b.name--插入
    insert into b
    select name,fraction
    from a t
    where not exists (select 1 from b where name = t.name)
    end
      

  3.   

    create table a(name varchar(10),fraction int,status bit)
    create table b(name varchar(10),fraction int)
    insert a
    select '张三',80,0 union all
    select '李四',70,0 union all
    select '王五',58,1
    insert b
    select '张三',20 union all
    select '王五',58
    go
    insert b(name,fraction) 
    select a.name,a.fraction from a 
    where not exists(select 1 from b where a.name=b.name)
    /*(所影响的行数为 1 行)*/
    go
    select * from b
    /*
    name  fraction
    ----   -----
    张三 20
    王五 58
    李四 70
    */
    go
    drop table a,b
      

  4.   

    为什么不用merge呢?多么强大一个语法
    有则更新,没有则插入,
    现在oracle 10G还可以删除。