HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

MS SQL Server, ask password before drop specific DB

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
sqlaskpassworddropspecificserverbefore

Problem

I've got 2 databases which are very important for our organization. I would like to prevent an accidental drop/delete of them... Do you have any suggestion? I was thinking about asking a password before perform the drop. Is this possible in MS SQL Server?

Solution

You should, as the comment says, rely on permissions for this.

Strictly speaking, is not possible to "ask password". Where would the password dialog even be displayed?? SQL Server is a client server product that accept commands from any client that uses the TDS protocol. That may be Management Studio, sqlcmd.exe, a PowerShell script, a ADO.Net SqlClient application. Anything.

If you must, then you can deploy, as a last resort, a DDL trigger. A DDL trigger will not replace permissions, but it can prevent an accidental drop.

CREATE TRIGGER safety 
ON SERVER
FOR DROP_DATABASE 
AS 
   PRINT 'You must disable Trigger "safety" to drop databases!' 
   ROLLBACK;


You can enhance the logic to check for database name, my purpose is not to give you a copy-paste working code.

Code Snippets

CREATE TRIGGER safety 
ON SERVER
FOR DROP_DATABASE 
AS 
   PRINT 'You must disable Trigger "safety" to drop databases!' 
   ROLLBACK;

Context

StackExchange Database Administrators Q#131588, answer score: 10

Revisions (0)

No revisions yet.