Solved

Deleting multiple sql databases

Posted on 2014-10-13
6
167 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 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

830 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