防止特定记录被删除 [英] preventing a specific record from being deleted
本文介绍了防止特定记录被删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想防止特定记录被删除。对于该特定记录,此触发器可以正常工作。但是,其他记录在删除时仍然保留。为什么?
I want to prevent a specific record from being deleted. This trigger works fine for that specific record. However, other records still remain when they're being deleted. Why?
ALTER TRIGGER [Globalization].[CountriesTracker]
ON [Globalization].[Countries]
INSTEAD OF DELETE
AS
BEGIN
SET NOCOUNT ON;
IF ((Select COUNT(*) from [Deleted]
Where [Deleted].[CountryId] = '36bd1536-fb56-4ec4-957e-1b3afde16c56') = 1)
BEGIN
RAISERROR('You can not delete this specific record!', 0, 0)
ROLLBACK TRANSACTION
RETURN
END
END
如何确保按预期删除不符合上述条件的行?
How can I ensure that rows not matching the above condition are being deleted as expected?
推荐答案
您有一个INSTEAD OF触发器,因此您需要在其中进行实际的删除。
You have an INSTEAD OF trigger so you need an actual DELETE in it.
我还将考虑简单地过滤保护行,因为:
I'd also consider simply filtering the protected row out because:
- 是否需要抛出错误?还是默默忽略?
- 删除包含受保护行的多行怎么办:中止整个操作,还是删除其余行?
类似的东西:
ALTER TRIGGER [Globalization].[CountriesTracker] ON [Globalization].[Countries]
INSTEAD OF DELETE
AS
SET NOCOUNT ON;
DELETE
CT
FROM
[Globalization].[Countries] C
JOIN
DELETED D ON C.CountryId = D.CountryId
WHERE
[Deleted].[CountryId] <> '36bd1536-fb56-4ec4-957e-1b3afde16c56'
GO
这篇关于防止特定记录被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文