Main menu

Database administration tricks

Here are some of my routine database scripts, collected from various sources. This can help database administrators to do day to day jobs. Finding the health of MSSQL server.

 

 

 

Find SQL Server information

//Check SQL server edition 
SELECT @@VERSION

//Get detailed information about Instance

SELECT  
    SERVERPROPERTY('productversion') as 'Product Version', 
    SERVERPROPERTY('productlevel') as 'Product Level',  
    SERVERPROPERTY('edition') as 'Product Edition',
    SERVERPROPERTY('buildclrversion') as 'CLR Version',
    SERVERPROPERTY('collation') as 'Default Collation',
    SERVERPROPERTY('instancename') as 'Instance',
    SERVERPROPERTY('lcid') as 'LCID',
    SERVERPROPERTY('servername') as 'Server Name'

Find all database size

with fs
as
(
    select database_id, type, size * 8.0 / 1024 size
    from sys.master_files
)
select 
    name,
    (select sum(size) from fs where type = 0 and fs.database_id = db.database_id) DataFileSizeMB,
    (select sum(size) from fs where type = 1 and fs.database_id = db.database_id) LogFileSizeMB
from sys.databases db order by DataFileSizeMB desc

Delete all databases

EXEC sp_MSforeachdb '
IF DB_ID(''?'') > 4
BEGIN
ALTER DATABASE [?] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DROP DATABASE [?]
END'

 

FacebookTwitterGoogle+RSS