Skip to content

Instantly share code, notes, and snippets.

@pradeep1288
Last active August 29, 2015 13:56
Show Gist options
  • Save pradeep1288/9198888 to your computer and use it in GitHub Desktop.
Save pradeep1288/9198888 to your computer and use it in GitHub Desktop.
Gets the size of all the databases on a given sql server instance. (Credits: http://goo.gl/4aw6Al)
SELECT @@SERVERNAME AS SqlServerInstance,
db.name AS DatabaseName,
SUM(CASE WHEN af.groupid = 0 THEN 0 ELSE 8192.0E * af.size / 1048576.0E END) AS DatabaseSize,
SUM(CASE WHEN af.groupid = 0 THEN 8192.0E * af.size / 1048576.0E ELSE 0 END) AS LogSize,
SUM(8192.0E * af.size / 1048576.0E) AS TotalSize
FROM master..sysdatabases AS db
INNER JOIN master..sysaltfiles AS af ON af.[dbid] = db.[dbid]
WHERE db.name NOT IN('distribution', 'Resource', 'master', 'tempdb', 'model', 'msdb')
GROUP BY db.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment