日期:2014-05-18 浏览次数:20472 次
create trigger tr_ins_opera on opera instead of insert as begin if exists (select 1 from inserted where mark='取款') begin if exists (select 1 from inserted a inner join uesr b on a.id = b.id and a.mark='取款' and a.change>b.money) begin print '超出取款金额' rollback tran end update uesr set money = money - change from uesr a inner join inserted b on a.id = b.id and b.mark='取款' end if exists (select 1 from inserted where mark='存款' ) begin update uesr set money = money + change from uesr a inner join inserted b on a.ID = b.id and b.mark='存款' end end
------解决方案--------------------
create trigger t1 on opera for insert as begin declare @s varchar(10),@t int select @s=mark from inserted if(@s='取款') begin if not exists(select 1 from user join inserted on user.id=inserted.id and money>change) rollback else update user set money=money-change from user join inserted on user.id=inserted.id end else update user set money=money+change from user join inserted on user.id=inserted.id end