如何拒绝对所有用户的表删除

时间:2023-02-22
本文介绍了如何拒绝对所有用户的表删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 SQL Server 2005 中,是否有一种方法可以使用单个语句拒绝删除行在数据库的所有用户的特定表中?

Within SQL Server 2005, is there a way, with a single statement, to deny delete on rows in a particular table for all users of the database?

推荐答案

试试这个:

CREATE TRIGGER yourTriggerName ON YourTableName
INSTEAD OF DELETE
AS

    ROLLBACK
    RAISERROR('ERROR, DELETEs not permitted in YourTableName!!!',16,1)
    RETURN

go

工作示例:

CREATE TABLE XYZ  (RowID int)
INSERT XYZ VALUES(1)
INSERT XYZ VALUES(2)
go 

CREATE TRIGGER yourTriggerName ON XYZ
INSTEAD OF DELETE
AS

    ROLLBACK
    RAISERROR('ERROR, DELETEs not permitted in XYZ!!!',16,1)
    RETURN

go

delete XYZ

输出:

Msg 50000, Level 16, State 1, Procedure yourTriggerName, Line 6
ERROR, DELETEs not permitted in XYZ!!!
Msg 3609, Level 16, State 1, Line 1
The transaction ended in the trigger. The batch has been aborted.

这篇关于如何拒绝对所有用户的表删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:XQuery 在单个 SQL 更新命令中添加或替换属性 下一篇:更改表以根据 Order By 添加 Identity 列

相关文章

最新文章