Solved

Deleting multiple sql databases

Posted on 2014-10-13
6
168 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 143

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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 143

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

738 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