有一表A:
ID,数量,月份
现在根据时间月份往表里更改数据,如果有某月份的数据,则是更新,否则插入.即要每项根据时间循环判断是否存在数据,有则更新,不然就插入,通过参数传入语句,全部过程在数据库中用语句判断
ID,数量,月份
现在根据时间月份往表里更改数据,如果有某月份的数据,则是更新,否则插入.即要每项根据时间循环判断是否存在数据,有则更新,不然就插入,通过参数传入语句,全部过程在数据库中用语句判断
调试欢乐多
@Count int,
@Month varchar(10)
as
update a set 数量=数据量+@Count where 月份=@Month
if @@rowcount=0
insert a(数量,月份) values(@count,@Month)
GO
insert tb select * from ta a where not exists(select 1 from tb where 月份 =a.月份 )
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的操作是,更新,插入,插入
insert into tb values...
else
update tb set shuliang = 某数量 where id = 某ID and convert(varchar(7),yuefen,120) = 某月份
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