The answer(s) provided here return the size of the database files, which is not necessarily the size of the data in the database. To get the size of data in each database on a SQL Server, use the following query.
DECLARE @r TABLE( name SYSNAME, size NUMERIC(36, 2) )
DECLARE @db SYSNAME
, @sql NVARCHAR(MAX)
, @size NUMERIC(36,2)
--
SELECT @db = MIN( name ) FROM sys.databases
--
WHILE @db IS NOT NULL
BEGIN
SET @sql = N'SELECT @size = CAST( ROUND( ( SUM( a.used_pages ) * 8 / 1024.00 ), 2) AS NUMERIC( 36, 2) )
FROM ' + @db + '.sys.tables t
LEFT JOIN ' + @db + '.sys.indexes i
ON t.OBJECT_ID = i.object_id
LEFT JOIN ' + @db + '.sys.partitions p
ON i.object_id = p.OBJECT_ID
AND i.index_id = p.index_id
JOIN ' + @db + '.sys.allocation_units a
ON p.partition_id = a.container_id'
--
EXEC sp_executesql @sql, N'@size NUMERIC(36,2) OUTPUT', @size OUTPUT
--
INSERT INTO @r
SELECT @db, @size
--
SELECT @db = MIN( name ) FROM sys.databases WHERE name > @db
END
--
SELECT name, size AS [size (MB)] FROM @r ORDER BY 2 DESC