views:

162

answers:

5

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%'

+1  A: 

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.

Ralph Rickenbach
A: 

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 ;)

devio
There isn't actually too much wrong with this. A cursor would be quite reliable in this situation.
Rob Farley
funny then that it got downvoted ^^
devio
I think some people probably downvote anything that suggests a cursor.
Rob Farley
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.
Rob Farley
A: 
Tuzo
this will not work for more than 1 row :)
Greco
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.
Ralph Rickenbach
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. :)
Tuzo
A: 
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
Greco
Downvoted for what?!?Cool.
Greco
+6  A: 

How about:

DECLARE @qry nvarchar(max);

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

EXEC sp_executesql @qry;

Rob

Rob Farley
I was going to write my own answer with a loop and then spotted this answer - very neat way of doing it Rob!
Chris W
Since FOR XML PATH('') generates no wrapper element, it is not necessary in the query. Otherwise, very nice. :)
Tuzo
Tuzo - actually, it's FOR XML PATH('') that provides the string concatenation, so that this statement will drop all matching databases.
Rob Farley