不允许删除表数据的触发器

USE [AdventureWorks]
GO

CREATE TRIGGER [HumanResources].[dEmployee] ON [HumanResouces].[Employee] 
INSTEAD OF DELETE NOT FOR REPLICATION AS 
BEGIN 
SET NOCOUNT ON;


DECLARE @DeleteCount int;


SELECT @DeleteCount = COUNT(*) FROM deleted;
IF @DeleteCount > 0 
BEGIN
RAISERROR
(N'Employees cannot be deleted .They can only be marked as not current .',--message
10,--severity.
1);--State.

--回滚事物
IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION;
END
END;
END;

GO

 

posted @ 2011-12-26 23:30  韩梦芫  阅读(622)  评论(0编辑  收藏  举报