还有你这不会是要求一个SQL语句出来吧~~~

解决方案 »

  1.   

    两条SQL语句还不一定可以达到要求,还要判断C表是否有数据,没有则Insert,有则Update,除非你创建了触发器
    不要那么苛刻了,用游标吧,挺简单的
      

  2.   

    delete from a where a.bh not in (select a.bh form a,b where a.bh=b.bh) 
    用游标吧
      

  3.   

    我是用pl/sql写的,大概的程序是这样的,你可以加一些异常处理就可以了declare
        变量定义省略。。    
        CURSOR cur_a IS 
            select bh,sl from a
            where bh in 
            ( select bh from a 
              minus
              select bh from b);
    begin  
       open cur_a;   
       loop
         FETCH cur_ar INTO v_bh,v_sl;     
         EXIT WHEN cur_a%NOTFOUND;     
         select count(*) into v_bz from c where bh = v_bh;
         if v_bz < 1 then
            insert into c(bh ,sl) values(v_bh ,(0 -v_sl));
         else
            update c set sl =sl-v_sl   where bh = v_bh;
         end if;     
         delete from  a where bh = v_bh;   
       end loop;   
       close cur_a;   
    end ;
      

  4.   

    sql server:update c 
      set sl=c.sl-a.sl
      from c,a
      where c.id=a.id
      and a.id not in (select id from b)insert c (id,sl)
      select id,-sl from a
      where a.id not in (select id from b) 
      and a.id not in (select id from c)delete a
      where id not in (select id from b)     
    如果数据很多,这些语句应该可以优化,数据少的话就不必了!
      

  5.   

    我的id字段是三个段联合起来的。 
    那我该怎样写。
    bh+cbh+dbh 才确定是这条记录。 
    其中
    bh,是int类型, cbh, dbh是字符类型
    是不是通过convert写, 可以连起来吗? 这个小问题解决,立即放分。谢谢各位高手,真是太感谢了。
      

  6.   

    这样呀!那干脆把优化一起做了:update c 
      set sl=c.sl-a.sl
      from c,a
      where c.bh=a.bh and c.cbh=a.cbh and c.dbh=a.dbh
      and not exists (select 1 from b where 
         b.bh=a.bh and b.cbh=a.cbh and b.dbh=a.dbh)insert c (bh,cbh,dbh,sl)
      select bh,cbh,dbh,-sl from a
      where not exists (select 1 from b where 
         b.bh=a.bh and b.cbh=a.cbh and b.dbh=a.dbh)
      and not exists (select 1 from c where 
         c.bh=a.bh and c.cbh=a.cbh and c.dbh=a.dbh)delete a
      where not exists (select 1 from b where 
         b.bh=a.bh and b.cbh=a.cbh and b.dbh=a.dbh)其实,也可以用convert(varchar(10),c.bh)+'|'+rtrim(c.cbh)+'|'+rtrim(c.dbh)代替c.id,但是这样写太麻烦,而且效率低。