How to get table size in a database?

This query will return size of each table in a sql database. This is a great way to monitor growth of a database and decide what restrictions might be needed to keep the database size from growing out of control. You may want to put a limit on file upload size on a table that might storing documents for example.

SELECT
t.NAME AS TableName,
p.rows AS TableRowCounts,
CONVERT(DECIMAL,SUM(a.total_pages)) * 8 / 1024 AS TotalSpaceMB,
CONVERT(DECIMAL,SUM(a.total_pages)) * 8 / 1024 /1024 AS TotalSpaceGB,
SUM(a.used_pages) * 8 / 1024 AS UsedSpaceMB ,
SUM(a.used_pages) * 8 / 1024 / 1024 AS UsedSpaceGB
FROM
DatabaseName.sys.tables t
INNER JOIN
DatabaseName.sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
DatabaseName.sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN
DatabaseName.sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN
DatabaseName.sys.schemas s ON t.schema_id = s.schema_id
WHERE
t.is_ms_shipped = 0
AND i.OBJECT_ID > 255
GROUP BY
t.Name, s.Name, p.Rows
ORDER BY
UsedSpaceGB DESC, t.Name