假设我有下表:
PKID | UID | FKID
-----------------
1 | ABC | 1
2 | BCD | 2
3 | CDE | 2
4 | DEF | 1
5 | EFG | 3
我想要做的是使用触发器阻止删除(或者如果有更好的方法可以采用其他方式),但仅限于FKID = 1但仍允许删除其他行的行。因此,如果某人键入DELETE FROM sampleTable
,我只希望删除第2,3和5行,并且应保留1和4。
答案 0 :(得分:0)
试试这个。
CREATE TRIGGER dbo.trg_tablename_delete ON dbo.tablename
FOR DELETE
AS
SET NOCOUNT ON
IF EXISTS (SELECT * FROM deleted WHERE FKID = 1)
BEGIN
RAISERROR ('Cannot delete this record!', 0, 1) WITH NOWAIT
ROLLBACK
END
SET NOCOUNT OFF
GO