Hi All,
I had requirement to clean up the SQL Server instance's database of old installation before the new installation. which I have spent almost of 2 hours for exploring the internet to find out there is any script which I can run in setup before attach of new db into the SQL instance. Finally I have found the very good post
http://rdineshkumar.wordpress.com/2011/12/17/t-sql-how-to-drop-all-databases-in-sql-server/
which clean the SQL instance of user database but it had problem which i have corrected and works fine.
Below is the SQL script.
DECLARE @sql NVARCHAR (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', 'mssecurity')
OPEN DBS
FETCH next FROM DBS INTO @DBname
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = 'DROP DATABASE ' + '"' + @DBname + '"'
PRINT @sql
EXECUTE sp_executesql @sql
FETCH next FROM DBS INTO @DBname
END
CLOSE DBS
DEALLOCATE DBS
Feel free to contact me for any problem with the script.
I had requirement to clean up the SQL Server instance's database of old installation before the new installation. which I have spent almost of 2 hours for exploring the internet to find out there is any script which I can run in setup before attach of new db into the SQL instance. Finally I have found the very good post
http://rdineshkumar.wordpress.com/2011/12/17/t-sql-how-to-drop-all-databases-in-sql-server/
which clean the SQL instance of user database but it had problem which i have corrected and works fine.
Below is the SQL script.
DECLARE @sql NVARCHAR (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', 'mssecurity')
OPEN DBS
FETCH next FROM DBS INTO @DBname
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = 'DROP DATABASE ' + '"' + @DBname + '"'
PRINT @sql
EXECUTE sp_executesql @sql
FETCH next FROM DBS INTO @DBname
END
CLOSE DBS
DEALLOCATE DBS
Feel free to contact me for any problem with the script.
No comments:
Post a Comment