Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get overall sum of all databases size in a SQL Server

I want to calculate how much space my databases are using in a server. I could use sp_spacefiles or query sys.databases table but that would give me separate results for each database and I would have to copy that into an excel sheet and calculate the sum from there.

Is there a direct way of doing it in T-SQL?

Thanks.

like image 823
olmed0 Avatar asked Aug 19 '11 13:08

olmed0


People also ask

How can I get total database size in SQL Server?

If you need to check a single database, you can quickly find the SQL Server database sizein SQL Server Management Studio (SSMS): Right-click the database and then click Reports -> Standard Reports -> Disk Usage. Alternatively, you can use stored procedures like exec sp_spaceused to get database size.

How would you calculate the entire database size?

To estimate the size of a database, estimate the size of each table individually and then add the values obtained. The size of a table depends on whether the table has indexes and, if they do, what type of indexes.

How do I sum total in SQL?

If you need to add a group of numbers in your table you can use the SUM function in SQL. This is the basic syntax: SELECT SUM(column_name) FROM table_name; If you need to arrange the data into groups, then you can use the GROUP BY clause.


1 Answers

You can query master.sys.master_files:

SELECT CONVERT(DECIMAL(10,2),(SUM(size * 8.00) / 1024.00 / 1024.00)) As UsedSpace
FROM master.sys.master_files

This will give you a total in GB.

Sys.Master_files is a server-wide view that lists every file in every DB. It's available from SQL Server 2005 onward.

like image 53
JNK Avatar answered Sep 27 '22 23:09

JNK