Wednesday, September 4, 2013

Clean up the databses of SQL Server Instance

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.

Digest of Design Patterns

I have started learning design pattern in real scenario of use. which will helps to realize how the design pattern works and solves our very complex problems in object oriented way.

There are three categories of design patterns
1) Creational
2) Structural
3) Behavioral

I have started with AbstractFactory pattern of creational categories.