Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deny delete on a table for all users

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?

like image 661
Malcolm Avatar asked Sep 03 '10 04:09

Malcolm


People also ask

How do I restrict a table in SQL?

You can restrict access to data at the following levels: You can use the GRANT and REVOKE statements to give or deny access to the database or to specific tables, and you can control the kinds of uses that people can make of the database.

Does dropping a table delete the data?

The DROP TABLE is another DDL (Data Definition Language) operation. But it is not used for simply removing data from a table; it deletes the table structure from the database, along with any data stored in the table.

What is the clause to delete all rows from the table?

The truncate command removes all rows of a table.

Which SQL command is use to remove a table all data?

The DROP TABLE statement is used to drop an existing table in a database.


1 Answers

try this:

CREATE TRIGGER yourTriggerName ON YourTableName
INSTEAD OF DELETE
AS

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

go

working sample:

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

OUTPUT:

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.
like image 111
KM. Avatar answered Sep 20 '22 08:09

KM.