dim db as adodb.connection
dim re&,sql$,name$,total&
set db=new adodb.connection
db.open 你的数据库连接字符串
'你要插入的值
name="aa"
total=10
'数据插入处理
sql="update 你的表 set total=total+" & total & " where name='" & name & "'"
db.execute sql,re
if re=0 then 're是处理的记录数,如果为0,表示表中没有name,这时进行新增
sql="insert into 你的表(name,total) values('" & name & "'," & total & ")"
db.execute sql,re
end if
--创建测试表
create table tb(id int identity(1,1),name varchar(10),total int)
go
--创建处理的触发器
create trigger t_insert on tb
instead of insert
as
update tb set total=isnull(a.total,0)+isnull(b.total,0)
from tb a join inserted b on a.name=b.name
insert into tb
select a.name,a.total from inserted a left join tb b on a.name=b.name
where b.name is null
go
--插入数据测试
insert into tb values('aa',2)
insert into tb values('aa',2)
insert into tb values('bb',2)
--显示结果
select * from tb
go
--删除测试表
drop table tb
/*--测试结果
id name total
----------- ---------- -----------
1 aa 4
2 bb 2
--用触发器就行了.
--创建处理的触发器
create trigger t_insert on tb
instead of insert
as
update tb set total=isnull(a.total,0)+isnull(b.total,0)
from tb a join inserted b on a.name=b.name
insert into tb
select a.name,a.total from inserted a left join tb b on a.name=b.name
where b.name is null
go