title: Select SQL Server database size
body: <p>how can i query my sql server to only get the size of database? </p>
  
  <p>I used this : </p>
  
  <pre><code>use "MY_DB"
  exec sp_spaceused
  </code></pre>
  
  <p>I got this :</p>
  
  <pre><code>database_name   database_size   unallocated space
  My_DB           17899.13 MB 5309.39 MB
  </code></pre>
  
  <p>It returns me several column that i don't need, maybe there is a trick to select database_size column from this stored procedure ? </p>
  
  <p>I also tried this code :</p>
  
  <pre><code>SELECT DB_NAME(database_id) AS DatabaseName,
         Name AS Logical_Name,
         Physical_Name,
         (size * 8) / 1024 SizeMB
  FROM sys.master_files
  WHERE DB_NAME(database_id) = 'MY_DB'
  </code></pre>
  
  <p>It gives me this result:</p>
  
  <pre><code>DatabaseName    Logical_Name    Physical_Name                    SizeMB
  MY_DB           MY_DB           D:\MSSQL\Data\MY_DB.mdf          10613
  MY_DB           MY_DB_log       D:\MSSQL\Data\MY_DB.ldf          7286
  </code></pre>
  
  <p>So i wrote this: </p>
  
  <pre><code>SELECT SUM(SizeMB)
  FROM (
      SELECT DB_NAME(database_id) AS DatabaseName,
             Name AS Logical_Name,
             Physical_Name,
             (size * 8) / 1024 SizeMB
      FROM sys.master_files
      WHERE DB_NAME(database_id) = 'MY_DB'
  ) AS TEMP
  </code></pre>
  
  <p>I got: 1183</p>
  
  <p>So it works but maybe there is a proper way to get this? </p>