0

I had a database Coderr that I no longer wanted. Unfortunately, I detached it instead of dropping it. Now I'm seeing traces of it pop up in SSMS dialogs:

enter image description here

I'd like to remove this (and any others that may be lurking).

I tried DROP DATABASE [Coderr], but I got this error:

Cannot drop the database 'coderr', because it does not exist or you do not have permission

I found this Q&A, but the T-SQL over there is a bit over my head and I'm nervous running it since I don't know whether it applies to my scenario.

How can I permanently clean all references to this old, unwanted database from my server? Will it be possible to do so, or am I stuck with the consequences of my goof-up?

Marcello Miorelli
  • 17,274
  • 53
  • 180
  • 320
InteXX
  • 559
  • 2
  • 7
  • 17

1 Answers1

1

I'd like to remove this (and any others that may be lurking).

I tried DROP DATABASE [Coderr], but I got this error:

Cannot drop the database 'coderr', because it does not exist or you do not have permission

Once you detach the database, it technically no longer exists in your instance of SQL Server (despite the file existing physically on disk still).

If I was you, your safest best would probably be to just re-attach it, then properly drop it. The scripts in the linked post's answer are rather safe to run after you've re-attached the database.

The first set of scripts just backups the database, closes any open connections to it, and then drops it. Just make sure you replace [db_name] with Coderr. John Eisbrener's code from that answer for historical reference:

-- Use master db to ensure you don't have an active connection to the db you wish to affect
USE [master]
GO

-- This will kill any active transactions, but will force the database into a Read-Only state ALTER DATABASE [db_name] SET READ_ONLY WITH ROLLBACK IMMEDIATE GO

BACKUP DATABASE [db_name] -- Fill in more options here or use the UI to take a backup if you chooose GO

-- This will kick out all connections from the database allowing you to drop it. ALTER DATABASE [db_name] SET SINGLE_USER WITH ROLLBACK IMMEDIATE GO

-- Drop the database (which automatically removes the files from the OS) DROP DATABASE [db_name] GO

The second set of scripts isn't modifying anything, rather it just gets some meta-data about Logins that were referencing the database you just dropped. John's code, once again for historical reference:

DECLARE @ExecString NVARCHAR (4000)

-- Create Empty Table in a very lazy manner SELECT name, principal_id, CAST('' AS NVARCHAR(128)) as database_name INTO ##tmp_AllDBUsers FROM sys.server_principals WHERE 1 = 2

-- Declare Cursor to iterate through all DBs on the instance DECLARE dbCursor CURSOR FOR SELECT name FROM sys .databases

DECLARE @name NVARCHAR (128) OPEN dbCursor FETCH NEXT FROM dbCursor INTO @name

WHILE @@FETCH_STATUS = 0 BEGIN

SET @ExecString = 
'USE [' + @name + '];
INSERT INTO ##tmp_AllDBUsers
SELECT sp.name, sp.principal_id, DB_NAME()
FROM sys.server_principals sp INNER JOIN sys.database_principals dp
    ON sp.sid = dp.sid'

EXEC(@ExecString)

FETCH NEXT FROM dbCursor
INTO @name

END

-- Close and deallocate the cursor because you've finished traversing all it's data CLOSE dbCursor DEALLOCATE dbCursor

-- Show all logins that do not belong to a server-level role nor have access to any databases SELECT sp.* FROM sys.server_principals sp LEFT JOIN ##tmp_AllDBUsers adu ON sp.principal_id = adu.principal_id WHERE adu.principal_id IS NULL AND sp.principal_id NOT IN (SELECT member_principal_id FROM sys.server_role_members) AND TYPE IN ('S', 'U', 'G')

-- cleanup DROP TABLE ##tmp_AllDBUsers

You should be safe running both, so long as you put the correct database name in the first set of scripts.


How about somebody engage in some actual constructive criticism, instead of just rudely downvoting and voting to close?

Unfortunately them's the breaks when using a free answer service, but I wouldn't take it personally. If I were to speculate, whoever downvoted you might've found your question trivial, but that's neither here nor there.

J.D.
  • 40,776
  • 12
  • 62
  • 141