读书人

更新的有关问题~

发布时间: 2012-01-29 21:39:32 作者: rapoo

更新的问题~~
比如有一个表 a
Name Value
a 2
a 3
a 4
b 1
b 2
b 3
有另外一个表 b 部分是由 a的到的
Name AllValue OtherValue
a 10 ewfefasf
b 6 fafdas

如何在更新表a 的时候 自动更新表 b了, 用触发器要怎么写了 ,当然b 表OtherValue不能变, 还有在a中添加一条 c 6 的记录
在 b表中也自动增加记录c


给个提示 写个触发器例子,或者提供别的思路也可以


[解决办法]
少看了个
create trigger abc on a for insert,update as
if exists(select 1 from b where name in(select name from a))
update b set AllValues=(select sum(Value) from a) where name=(select name from inserted)
else
insert b select * from inserted
go

[解决办法]
create trigger tri_upins on a --在A表建立触发器
after insert,update
as
if update(Value) or update(Name) --判断用户是否在这两个列上进行了更新操作
update b set AllValue=(select sum(Value) from a) where name=(select Name from inserted) --重新进行统计,条件是inserted 临时表的Name字段
if exists(select * from inserted) --判断用户是否插入了数据
insert into b select * from inserted

[解决办法]
如果同时插入:

insert a select 'b ',3 union select '3 ' , 3 union select 'c ' , 5 这个触发器可以成功吗?

读书人网 >SQL Server

热点推荐