欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SqlServer實(shí)現(xiàn)類似Oracle的before觸發(fā)器示例

 更新時(shí)間:2014年08月01日 09:43:41   投稿:whsnow  
本節(jié)主要介紹了SqlServer如何實(shí)現(xiàn)類似Oracle的before觸發(fā)器,需要的朋友可以參考下

1. 插入數(shù)據(jù)前判斷數(shù)據(jù)是否存在

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
-- ============================================= 
-- Author: <Author,,Name> 
-- Create date: <Create Date,,> 
-- Description: <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. 刪除表中數(shù)據(jù)時(shí)需要先刪除外鍵表的數(shù)據(jù)

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
-- ============================================= 
-- Author: <Author,,Name> 
-- Create date: <Create Date,,> 
-- Description: <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

相關(guān)文章

最新評(píng)論