Sunday, 19 January 2014

SQL Server - How to Get Database Size in SQL Server 2008

Here I will explain how to get database size in SQL serveror query to get database size in SQL server 2008.
Description:

In previous articles I explained show time difference in minute ago hour ago etc in SQL Server, get list of procedures where table name used in sql, difference b/w view and stored procedure in sql, add row items as string with comma separated values in sql server and many articles relating to SQL server. Now I will explain how to get database size in SQL server.

To get database size in SQL server we need to write the query like as shown below


SELECT
database_name = DB_NAME(database_id)
, log_size_mb = CAST(SUM(CASE WHEN type_desc = 'LOG' THEN size END) * 8. / 1024 AS DECIMAL(8,2))
, row_size_mb = CAST(SUM(CASE WHEN type_desc = 'ROWS' THEN size END) * 8. / 1024 AS DECIMAL(8,2))
, total_size_mb = CAST(SUM(size) * 8. / 1024 AS DECIMAL(8,2))
FROM sys.master_files WITH(NOWAIT)
WHERE DB_NAME(database_id) = 'master' –-- your database name
GROUP BY database_id
Once we run above code we will get output like as shown below

Demo



No comments:

Post a Comment

Note: only a member of this blog may post a comment.