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.
Legacy Comments
Raju Joseph
2007-03-01 |
re: Gather a Database Inventory I would use the new catalog views - sys.master_files and sys.databases. Also, it would be nice if I could also see the status of the database as well (ONLINE, EMERGENCY, etc.). |
Phil
2007-03-02 |
re: Gather a Database Inventory Thanks for the tip Raju. I will play around with the script and see if I can add those columns. Stay tuned |
TOm
2007-05-13 |
re: Gather a Database Inventory calling it file_name is a little bit tricky. The sysaltfiles.filename is the physical file name which you may also want to output. |
Joe
2007-07-23 |
re: Gather a Database Inventory Thanks for the script. Work great. |
Soumadip Banerjee
2007-10-29 |
re: Gather a Database Inventory Add To total size of the Database |
Patrick
2008-05-29 |
re: Gather a Database Inventory This works great but if you are admin of several SQL servers can you write this to get the data from all servers on a domain? |
mukhtar aqeel
2009-06-29 |
re: Gather a Database Inventory salam |