加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

SQLServer触发器实例

发布时间:2020-12-12 12:50:09 所属栏目:MsSql教程 来源:网络整理
导读:?????? 以前没写过SqlServer的触发器,折腾了一天,总算写好了,现在记录一下心得。 ?????? SqlServer触发器的语法就不提了,主要记录一下主要事项。每个开始都得有Begin? 内容? end?? ,之前不明白这里,写条件触发折腾了好多遍都是不准确,以下是实例: ??

?????? 以前没写过SqlServer的触发器,折腾了一天,总算写好了,现在记录一下心得。

?????? SqlServer触发器的语法就不提了,主要记录一下主要事项。每个开始都得有Begin? 内容? end?? ,之前不明白这里,写条件触发折腾了好多遍都是不准确,以下是实例:

?????

??????if (object_id('insert_black_list','tr') is not null)??? drop trigger insert_black_list
go
create TRIGGER insert_black_list
??? on User_Infor_Message
??? after insert
??? as
??? declare @messagetype varchar(1)
??? declare @cardno varchar(10)
??? BEGIN
???? select @messagetype = messagetype,@cardno=cardno from User_Infor_Message
???? if @messagetype='1' or @messagetype='5'
???????? begin
?????????? if (select count(cardno) from Balck_List where cardno=@cardno)=0
????????????? begin
??????????????? insert into Balck_List select Inserted.cardno,Inserted.cardid,convert(varchar(8),getdate(),112) from Inserted
????????????? end
???????? end
???? else if @messagetype='6'
?????? begin
????????? delete from Balck_List where cardno=@cardno ?????? end ??? END ?go

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读