Insert 触发器

来源:互联网 发布:苹果如何更改知乎话题 编辑:程序博客网 时间:2024/05/22 08:21

create Table Bank(
 CardID int primary key identity(1,1),
 BankUserName nvarchar(50),
 CurrentMomney decimal(10,2),
)
create Table Inserted(
 ID int primary key identity(1,1),
 CardID int foreign key(CardID) references Bank(CardID),
 TransType nvarchar(30),
 TransMoney decimal(10,2),
)
create trigger trig_TransInfo
on Inserted --表名
for insert --触发器类型
as
declare @type char(4),@outmoney decimal(10,2)
declare @mycardID int,@balance decimal(10,2)
select @type=TransType,@outmoney=TransMoney,@mycardID=CardID from Inserted
if(@type='支取')
 update Bank set CurrentMomney=CurrentMomney-@outmoney where CardID=@mycardID
else
 update Bank set CurrentMomney=CurrentMomney+@outmoney where CardID=@mycardID
go

 

insert into Bank values('zhansan',500)
insert into Bank values('lisi',10000)
insert into Bank values('wangwu',2000)


insert into Inserted values(1,'支取',100)

(1 行受影响)

(1 行受影响)

------------------------------------------------------
insert into Inserted values(2,'存储',500)

(1 行受影响)

(1 行受影响)

 

另外,update触发器和delete触发器和insert触发器基本相同。

原创粉丝点击