有一表A:
ID,数量,月份
现在根据时间月份往表里更改数据,如果有某月份的数据,则是更新,否则插入.即要每项根据时间循环判断是否存在数据,有则更新,不然就插入,通过参数传入语句,全部过程在数据库中用语句判断

解决方案 »

  1.   

    create proc P_Update_Insert
        @Count int,
        @Month varchar(10)
    as
        update a set 数量=数据量+@Count where 月份=@Month
        if @@rowcount=0
            insert a(数量,月份) values(@count,@Month)
    GO
      

  2.   

    update tb set tb.数量=a.数量 from ta a where exists(select 1 from tb where 月份 =a.月份 )
    insert tb select * from ta a where not exists(select 1 from tb where 月份 =a.月份  )
      

  3.   

    有很多项目的,不止一个对象的
    id   shuliang  yuefen 
    1    100       2008-01-01
    1    200       2008-02-01
    2    300       2008-01-01
    3    400       2008-01-01现在项目1的2个月份都了,2,3没有,但是要求都有2个月的, 现在有3行数据到表里,对项目1,2,3的操作是,更新,插入,插入 
      

  4.   

    if not exists(select 1 from tb where id = 某ID and convert(varchar(7),yuefen,120) = 某月份)
       insert into tb values...
    else 
       update tb set shuliang = 某数量 where id = 某ID and convert(varchar(7),yuefen,120) = 某月份
      

  5.   

    做一个触发器:
    create trigger tg on tb
    for insert,update
    as
    if exists(select 1 from inserted where 月份= month(getdate()))
     update tb set col=...
    else 
     insert tb ...
    go