Solved

Deleting multiple sql databases

Posted on 2014-10-13
6
166 Views
Last Modified: 2014-10-14
Hi Experts, I need to delete multiple databases (200+) which start with 2014 Oct - Dec_* where * is a random number and that number changes. Is there a query I can run to do that? SQL is 2008

TIA
0
Comment
Question by:abhijitm00
  • 3
  • 2
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40377840
using this basic code to process several record:
http://www.experts-exchange.com/Database/MS-SQL-Server/A_13640-processing-cursor-vs-temp-table-syntax.html

-- starting with the variables you will need in the processing part
declare @id int
declare @name sysname
declare @l nvarchar(max)

-- here is the table variable definition, which lives only for the duration of the run and is cleaned up automatically
-- for "small" results, it will stay purely in memory; larger sets may be "stored" in the temp database
declare @c table ( id int , name sysname )
-- fill the table with data, update your select as you need it
-- if your process requires some "order" in the processing, still an ORDER BY here will be useless (unless you also use and need a TOP X clause) see below on where you have to put the ORDER BY

set nocount on
insert into @c (id, name) select id, name from sysdatabases where name like '2014 Oct - Dec_%' ;

-- process, either if the above insert did at least 1 row, or if the below "delete" did indeed 
while @@ROWCOUNT <> 0
begin
  -- fetch 1 record from the table. 
  -- if your process requires some "order" in the processing, put the corresponding ORDER BY here
  select top 1 @id = id, @name = name from @c

  if @@ROWCOUNT <> 0
  begin
   -- process the data here, exactly the same as with the cursor example.
   set @l = 'DROP DATABASE [' + @name + ']'
   exec(@l)
  end

  -- here we delete the row from the temp table , which is nothing else than a processing queue table, hence
  delete @c where id = @id
END 

Open in new window

0
 

Author Comment

by:abhijitm00
ID: 40377970
Thanks Guy. I am getting this error:

Msg 207, Level 16, State 1, Line 13
Invalid column name 'id'.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40378125
IF OBJECT_ID('tempdb.dbo.#drop_database_commands') IS NOT NULL
    DROP TABLE #drop_database_commands
CREATE TABLE #drop_database_commands (
    command nvarchar(200) NOT NULL
    );

INSERT INTO #drop_database_commands
SELECT 'DROP DATABASE [' + name + ']; '
FROM sys.databases
WHERE name LIKE '2014 Oct - Dec_[0-9]%'

SELECT * FROM #drop_database_commands

--review the commands that are output, and if they are correct, run them.
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:abhijitm00
ID: 40378190
Hi Scott, the results are correct. How do I run the command to drop them now? Thanks
0
 

Author Comment

by:abhijitm00
ID: 40378208
Never mind, got it. Thanks
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40380663
sorry I got a small error in the code...
-- starting with the variables you will need in the processing part
declare @name sysname
declare @l nvarchar(max)

-- here is the table variable definition, which lives only for the duration of the run and is cleaned up automatically
-- for "small" results, it will stay purely in memory; larger sets may be "stored" in the temp database
declare @c table (  name sysname )
-- fill the table with data, update your select as you need it
-- if your process requires some "order" in the processing, still an ORDER BY here will be useless (unless you also use and need a TOP X clause) see below on where you have to put the ORDER BY

set nocount on
insert into @c ( name) select  name from sysdatabases where name like '2014 Oct - Dec_%' ;

-- process, either if the above insert did at least 1 row, or if the below "delete" did indeed 
while @@ROWCOUNT <> 0
begin
  -- fetch 1 record from the table. 
  -- if your process requires some "order" in the processing, put the corresponding ORDER BY here
  select top 1 @name = name from @c

  if @@ROWCOUNT <> 0
  begin
   -- process the data here, exactly the same as with the cursor example.
   set @l = 'DROP DATABASE [' + @name + ']'
   exec(@l)
  end

  -- here we delete the row from the temp table , which is nothing else than a processing queue table, hence
  delete @c where name = @name 
END 
                                          

Open in new window

0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question