Jump to content

Calling Sql Server Dba People, Need A Script To Clean Up The Database


Recommended Posts

Posted

What is the error that you are getting ?

 

drop database 7d1dc83f-f151-4afe-a0b5-4583ebf2312b_PROXY
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '7'.

Posted
DECLARE @sql VARCHAR (max)
DECLARE @DBname VARCHAR (50)
DECLARE DBS CURSOR FOR
SELECT name
FROM   sys.databases
WHERE  name NOT IN ( 'model', 'tempdb', 'master', 'model',
'reportserver', 'ReportServerDB', 'msdb')
OPEN DBS
FETCH next FROM DBS INTO @DBname
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = 'DROP DATABASE  [' + @DBname + ']'  -- Added Square brackets 
PRINT @sql
--exec @sql
FETCH next FROM DBS INTO @DBname
END
CLOSE DBS
DEALLOCATE DBS
Posted

 

DECLARE @sql VARCHAR (max)
DECLARE @DBname VARCHAR (50)
DECLARE DBS CURSOR FOR
SELECT name
FROM   sys.databases
WHERE  name NOT IN ( 'model', 'tempdb', 'master', 'model',
'reportserver', 'ReportServerDB', 'msdb')
OPEN DBS
FETCH next FROM DBS INTO @DBname
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = 'DROP DATABASE  [' + @DBname + ']'  -- Added Square brackets 
PRINT @sql
--exec @sql
FETCH next FROM DBS INTO @DBname
END
CLOSE DBS
DEALLOCATE DBS

 

 

let me give a try...
 

Posted

 

DECLARE @sql VARCHAR (max)
DECLARE @DBname VARCHAR (50)
DECLARE DBS CURSOR FOR
SELECT name
FROM   sys.databases
WHERE  name NOT IN ( 'model', 'tempdb', 'master', 'model',
'reportserver', 'ReportServerDB', 'msdb')
OPEN DBS
FETCH next FROM DBS INTO @DBname
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = 'DROP DATABASE  [' + @DBname + ']'  -- Added Square brackets 
PRINT @sql
--exec @sql
FETCH next FROM DBS INTO @DBname
END
CLOSE DBS
DEALLOCATE DBS

 

 

This one worked...

 

thanks bro....can u add the a condition to select/delete only the Databases created 100 days before...

 

i tried to add this  " create_date > GETDATE() - 100"   but it is not executing it...

Posted

Naaku koddiga time ivvu .. Leaving office . intiki vellka chepta.. 

Posted

Naaku koddiga time ivvu .. Leaving office . intiki vellka chepta.. 

 

ok sure..thanks..

Posted
DECLARE @sql VARCHAR (max)

DECLARE @DBname VARCHAR (50)

DECLARE DBS CURSOR FOR

SELECT name

FROM   sys.databases

WHERE  name NOT IN ( 'model', 'tempdb', 'master', 'model',

'reportserver', 'ReportServerDB', 'ReportServerTempDB','msdb') and create_date > getdate()-100

OPEN DBS

FETCH next FROM DBS INTO @DBname

WHILE @@FETCH_STATUS = 0

BEGIN

SET @sql = 'DROP DATABASE  [' + @DBname + ']'  -- Added Square brackets 

--PRINT @sql

exec (@sql)

FETCH next FROM DBS INTO @DBname

END

CLOSE DBS

DEALLOCATE DBS
Posted
DECLARE @sql VARCHAR (max)

DECLARE @DBname VARCHAR (50)

DECLARE DBS CURSOR FOR

SELECT name

FROM   sys.databases

WHERE  name NOT IN ( 'model', 'tempdb', 'master', 'model',

'reportserver', 'ReportServerDB', 'ReportServerTempDB','msdb') and create_date > getdate()-100

OPEN DBS

FETCH next FROM DBS INTO @DBname

WHILE @@FETCH_STATUS = 0

BEGIN

SET @sql = 'DROP DATABASE  [' + @DBname + ']'  -- Added Square brackets 

--PRINT @sql

exec (@sql)

FETCH next FROM DBS INTO @DBname

END

CLOSE DBS

DEALLOCATE DBS

 

the one which UV12 edited it worked man..

trying..

Posted

date condition adigav kadha andukani post chesa.. anyway you figured it out good..

the one which UV12 edited it worked man..

trying..

 

Posted

date condition adigav kadha andukani post chesa.. anyway you figured it out good..

 

hmm nenu last time date condition vesinappudu edo error vachindi..so adi correct ga ela add cheyalo ani adiga indaka..

 

now working fine..typo unde na dantlo..

Posted

oka SQL Server undi..

 

i need to delete all the databases created on it let say before May 31st 2014.

 

evarikaina idea unte..cheppandi

 daniki scrift enduk vayya...oka bottle clorox vaadu chalu bemmiRTlaugh.gif?1403645933

×
×
  • Create New...