我是靠谱客的博主 心灵美烧鹅,这篇文章主要介绍Insert 触发器,现在分享给大家,希望可以做个参考。

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触发器基本相同。

最后

以上就是心灵美烧鹅最近收集整理的关于Insert 触发器的全部内容,更多相关Insert内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部