我正在使用一个具有触发器的数据库,该触发器通过引发错误而不提交删除来防止删除某个表中的记录。如果要删除的记录的列具有特定值,我需要修改行为以允许删除。
这是当前代码:
CREATE TRIGGER [dbo].[MyTable_PreventDelete]
ON [dbo].[MyTable]
INSTEAD OF DELETE
AS
-- TODO: Only run the code below if Deleted = 0
ROLLBACK
RAISERROR('ERROR: That column may not be deleted.',16,1)
RETURN
GO
我试图简单地将错误调用包装在条件中,但似乎我不能简单地直接引用受影响行的列:
...
CREATE TRIGGER [dbo].[MyTable_PreventDelete]
ON [dbo].[MyTable]
INSTEAD OF DELETE
AS
IF IsDeleted = 0
BEGIN
ROLLBACK
RAISERROR('ERROR: That column may not be deleted.',16,1)
RETURN
END
GO