触发不从表中删除数据

时间:2014-07-31 00:00:26

标签: sql-server tsql sql-server-2012 triggers

我有一个名为PaperInfo的表,表中的示例数据是:

PaperID   Type   Description
1         T      Level 1
2         S      Level 2
3         V      Level 3

如何在没有人可以从上表中删除记录的情况下强制执行触发器。当某人试图删除它时,例如如下:

Delete from dbo.paperInfo 
WHERE PaperID = 1

它应显示错误消息“无法从Paperinfo表中删除记录”

我该怎么办?

1 个答案:

答案 0 :(得分:2)

你应该写一个INSTEAD OF触发器。这将取代删除操作:

CREATE TRIGGER td_PaperInfo 
    ON  PaperInfo 
    INSTEAD OF DELETE
AS 
BEGIN
    RAISERROR ('Cannot Delete Records from Paperinfo Table', 16, 1)
END