SQL Server Discovery

Better, faster, cheaper ...pick two.
posts - 13, comments - 80, trackbacks - 0

Wednesday, February 28, 2007

Gather a Database Inventory

As a DBA for a large corporation, I manage literally hundreds of databases. In order to help keep track of all the databases and log files, I needed a way to quickly gather an inventory of all databases located on each sql named instance, so I came up with this simple little script that also includes a filesize in megabytes column: 


USE [Master]
GO 
SELECT 
	sysdatabases.name AS 'db_name'  
,	(select 'file_type' =  
	CASE  
		WHEN sysaltfiles.groupid = 1 THEN 'data'
		WHEN sysaltfiles.groupid = 0 THEN 'log'
	END) as 'file_type'	
,	sysaltfiles.name AS 'file_name'
,	(sysaltfiles.size * 8 / 1024) AS 'file_size(MB)' -- file size in MB
FROM 
	dbo.sysdatabases
JOIN
	dbo.sysaltfiles ON
	(dbo.sysdatabases.dbid=dbo.sysaltfiles.dbid)
WHERE
	sysdatabases.dbid NOT IN ('1','2','3','4')
ORDER BY
	dbo.sysdatabases.name


Run this script in the Master database and it will output a database list that you can export to a spreadsheet for reporting or futher analysis. If anyone knows a better way to do this or some enhancement suggestions to include additional useful information, let me know.

posted @ Wednesday, February 28, 2007 9:40 AM | Feedback (7) |

Powered by:
Powered By Subtext Powered By ASP.NET