1

I have created a list of databases I want to drop based on a set of conditions and have titled it delete_table in the master database (this already excludes master, tempdb, model and msdb). The only column in this table is name which contain the exact database names that should be deleted.

How would I go about writing the script that would drop the databases on the server based on this list?

Thanks!

2 Answers 2

2

If you want a quick way to script out DROP DATABASE commands:

--set database in single user mode, rolling back active transactions...be careful!
SELECT 'ALTER DATABASE ' + QUOTENAME(name) + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE;' FROM sys.databases WHERE name IN (...)
UNION
--drop database
SELECT 'DROP DATABASE ' + QUOTENAME(name) + ';' FROM sys.databases WHERE name IN (...);

Replace ... with a comma separated list of database names (e.g N'DatabaseOne', 'DatabaseTwo', etc.). Run the select query, then use results for execution.

I don't understand the need for a user table to solve this. As a side note, you should avoid creating user objects in master database. Granted, I can't think of a reason other than one based on aesthetics, but it just seems wrong.

More on user-created objects in system databases...

MSDN states user objects shouldn't be created in master, but I think the reason provided is pretty weak. A more substantive argument involves lack of control over what happens to system database objects during service pack/version upgrades.

I'm not saying the next service pack upgrade will wipe out your user created objects in master, but who knows. Put your utility and administration-type objects in a user created database so there's no confusion.

Sign up to request clarification or add additional context in comments.

9 Comments

Agree on not creating user object in master.
What if I were to delete the tables I created after the script was completed?
I don't see why the table of database names is needed. Why not put your conditions directly in the query provided above? Tell us what your conditions are for dropping a database, and we can incorporate into the solution.
The way I have it working now is I create a table containing the date when the database was created and when it was last accessed. The condition I have set is that the currentdate - createdate/lastaccessdate > 30. The databases that meet this condition are listed down in the delete_table table. I'm fairly new to SQL and this is the only way I knew to go about doing this
OK, how are you determining the last access date?
|
1

quick way

DECLARE @SQL VARCHAR(MAX) = ''
SELECT @SQL = @SQL+ 'DROP DATABASE ' + QUOTENAME(name) + ';' 
FROM delete_table
EXECUTE (@SQL)

a bit safer way to make sure database exists and each database get scripted only once incase you have dups

DECLARE @SQL VARCHAR(MAX) = ''
SELECT @SQL = @SQL+ 'DROP DATABASE ' + QUOTENAME(name) + ';' 
FROM sys.databases WHERE name IN (SELECT name FROM delete_table); 
EXECUTE (@SQL) 

Edit to add alter database simply add this line

DECLARE @SQL VARCHAR(MAX) = ''
SELECT @SQL = @SQL+ 
'ALTER DATABASE '+ QUOTENAME(name) + 'SET SINGLE_USER WITH ROLLBACK IMMEDIATE;' +
'DROP DATABASE ' + QUOTENAME(name) + ';' 
FROM sys.databases WHERE name IN (SELECT name FROM delete_table); 
EXECUTE (@SQL)

2 Comments

Thanks! this works quite nicely. One last thing, is there a way to do the: ALTER DATABASE [database_name] SET SINGLE_USER WITH ROLLBACK IMMEDIATE command easily for all the databases?
I have added the "alter database set single user..."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.