SQL触发器可防止删除单行

时间:2019-02-01 17:35:18

标签: sql triggers

我有一个表,其中有一排数据,我不想让任何人删除它。表名称是ProjectInProcessData。我想确保ID 6050的数据不能删除。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

    Create Trigger [dbo].[triggerPreventDeleteFormula]
         ON [dbo].[AHSC_Project_InprocessData]
    INSTEAD OF DELETE 
    AS
    BEGIN
      IF EXISTS(
    SELECT * 
    FROM deleted d
    where d.AHSC_Project_InprocessData_ID = '6610' or   d.AHSC_Project_InprocessData_ID = '6666'
    )
   BEGIN
    ROLLBACK;
    RAISERROR('Can not delete this record: this record contains default formula',16,1);
   END
   ELSE
     BEGIN
      DELETE [AHSC_Project_InprocessData]
       WHERE EXISTS (Select * from deleted d where d.AHSC_Project_InprocessData_ID = [AHSC_Project_InprocessData].AHSC_Project_InprocessData_ID)
      END
   END

答案 1 :(得分:0)

在删除时使用where条件

delete  from ProjectInProcessData  where id!=6050