1

I need to delete databases whose name start with "In"...

I Tried "Like" but it is throwing syntax errors...

I tried following command but throwing syntax errors for "name"

SELECT 'DROP DATABASE ' + name + ';' from sys.databases where name like 'In%'

5 Answers 5

12

How about:

DECLARE @qry nvarchar(max);

SELECT @qry = 
(SELECT 'DROP DATABASE ' + QUOTENAME(name) + '; ' 
FROM sys.databases 
WHERE name LIKE 'In%'
FOR XML PATH(''));

EXEC sp_executesql @qry;
Sign up to request clarification or add additional context in comments.

5 Comments

I was going to write my own answer with a loop and then spotted this answer - very neat way of doing it Rob!
Since FOR XML PATH('') generates no wrapper element, it is not necessary in the query. Otherwise, very nice. :)
Tuzo - actually, it's FOR XML PATH('') that provides the string concatenation, so that this statement will drop all matching databases.
Just in case: for dropping mfd-s I needed to add the brackets for Filenames 'DROP DATABASE [' + name + ']; ' :-)
Yes, but QUOTENAME is nicer in case ] is in the name, so I’ve added that now.
1

The syntax is

DROP DATABASE { database_name | database_snapshot_name } [ ,...n ] [;]

It does not allow for wildcards or where clauses. You would have to do that manually or programmatically, getting the names of the databases from sys.databases.

Comments

0

you can create a CURSOR that loops over

SELECT name FROM sys.databases WHERE name LIKE 'ln%'

and inside the loop you create the drop statement and execute it

SET @s = 'DROP DATABASE ' + @name
EXEC (@s)

not sure whether that works, though, and I don't have any databases to drop to test this ;)

3 Comments

There isn't actually too much wrong with this. A cursor would be quite reliable in this situation.
I think some people probably downvote anything that suggests a cursor.
I've given you an upvote to cancel out the downvote. I think a cursor is just as fine as a WHILE loop anyway, and Greco's suggestion didn't seem to pick up a downvote.
0
USE master;

DECLARE @Temp TABLE (Id INT IDENTITY(1, 1), name nvarchar(max));
DECLARE @RowCounter INT;

INSERT INTO @Temp
select name from sys.databases where name like N'In%';

SELECT @RowCounter = MAX(Id) FROM @Temp;

DECLARE @i INT;
SET @i = 1;

DECLARE @DBDeleteByName NVARCHAR(max);

WHILE @i <= @RowCounter
BEGIN
    ----- Build the drop database sql.
    SELECT @DBDeleteByName = 'DROP DATABASE ' + name 
    FROM @Temp WHERE Id = @i;

    ----- Drop the database.
    EXEC sp_executesql @DBDeleteByName;

    ----- Increment counter
    SET @i = @i + 1;

END

Comments

0

You could create a SQL statement to generate a script for you:

SELECT 'DROP DATABASE ' + name + ';' from sys.databases where name like 'In%'

and then run that script.

So if you had two databases InTestDB1 and InTestDB2 the above would generate the following results:

DROP DATABASE InTestDB1;
DROP DATABASE InTestDB2;

You could then save that to a file to run as a script on another server or copy it directly into query analyzer to run immediately.

2 Comments

I would not see why not. It is semiautomatic, as the select's result rows will be multiple drop database directives yopu will have to copy and execute.
As Ralph said, multiple databases are supported. I like this approach to semi-automate generating a production script where it's very easy for the DBAs to see what the script does in advance. The question didn't provide context about where or how to drop the database, though. If I was going to pick on one thing it would be that all statements are in one batch. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.