求SQL 触发器一个

来源:百度知道 编辑:UC知道 时间:2024/06/28 05:10:38
表A UserStock
表B UserInfo
当表A.NowPrice字段 更改时 B.NowMony= A.NowPrice * A.BuyNum + B.TotalMoney

这两个表之间是通过什么关联的呢 假设是通过一个uid吧

if exists(select * from sysobjects where name = 'a_b_update')
drop trigger a_b_update
go

create trigger a_b_update
on a
for update
as
declare @uid int
declare @nowprice money
declare @buynum int
declare @totalmoney money

select @uid = uid,@nowprice = nowprice ,@buynum = buynum from inserted
begin
select @totalmoney = totalmoney from b where uid = @uid
update 表b set nowmoney = @nowprice * @buynum + @totalmoney
where uid= @uid
end
go