例如:A表有,字段:ID;Count
                    1   13
                    2    5
      B表有,字段:BID;BCount
                    1    5
                    2    6
现在在存储过程里想判断A表的Count和BCount的大小。这俩字段都没有参数带进来的。
想要实现的效果:
if(Count>BCount)
   insert.....
else
   update......

解决方案 »

  1.   

    --问题是,两个表中各有多条记录,它们有大有小,你以什么来比较呢?
    --ID 相同的进行比较是一个选择,那必须要有个ID:
    if exists(select 1 from A inner join B on a.id=b.id where a.id=@id and a.Count>b.Bcount)
      insert ....
    else
      update...
      

  2.   

    分两步走,无须存储过程.
    update ... from a , b where a.id = b.bid and a.Count <= b.bcount
    insert into ... select ... from a , b where a.id = b.bid and a.Count > b.bcount