SqlServer怎么实现类似Oracle的before触发器
本篇内容主要讲解“SqlServer怎么实现类似Oracle的before触发器”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“SqlServer怎么实现类似Oracle的before触发器”吧!
创新互联公司专注于企业全网整合营销推广、网站重做改版、河口网站定制设计、自适应品牌网站建设、H5响应式网站、商城开发、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为河口等各大城市提供网站开发制作服务。
1. 插入数据前判断数据是否存在
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author:-- Create date: -- Description: -- ============================================= alter TRIGGER CategoryExistTrigger ON ProductCategory instead of insert AS declare @categoryName varchar(50); BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for trigger here select @categoryName = CategoryName from inserted; if exists(select * from ProductCategory where CategoryName =@categoryName) begin print 'Category exists..' end; else begin insert into ProductCategory select * from inserted; end; END
2. 删除表中数据时需要先删除外键表的数据
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author:-- Create date: -- Description: -- ============================================= alter TRIGGER DeleteOrderTrigger ON OrderHeader instead of delete AS declare @OrderId varchar(50); BEGIN SET NOCOUNT ON; select @OrderId = OrderId from deleted; delete from OrderLine where OrderId = @OrderId; END GO
到此,相信大家对“SqlServer怎么实现类似Oracle的before触发器”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
当前名称:SqlServer怎么实现类似Oracle的before触发器
URL网址:http://pwwzsj.com/article/pdcgse.html