Hi Friends!
So, how we are going to find out table size information of a given database?
Here we go….
Query 1

In this, we have to first select DATABASE from the SQL Server and fire the query so as to get the information about current size and free space of the selected database.
Let's say I have selected master database as shown below.
SQL Server database
Now, execute the following query on it.
  1. --Database size information
  2. SELECT Db_name()AS dbname,
  3. name AS filename,
  4. size / 128.0 AS currentsizemb,
  5. size / 128.0 - CAST(Fileproperty(name,'SpaceUsed') AS INT) / 128.0 AS freespacemb
  6. FROM sys.database_files
We will get the output in the following format.
SQL Server Database Size
Query 2
This query is informative and contains almost all the required information that we require about any table.
  1. –Detailed Database size information
  2. DECLARE
  3. @max INT,
  4. @min INT,
  5. @owner NVARCHAR(256),
  6. @table_name NVARCHAR(256),
  7. @sql NVARCHAR(4000)
  8. DECLARE @table TABLE(
  9. ident INT IDENTITY(1,1) PRIMARY KEY,
  10. owner_name NVARCHAR(256),
  11. table_name NVARCHAR(256))
  12. IF (SELECT OBJECT_ID('tempdb..#results')) IS NOT NULL
  13. BEGIN
  14. DROP TABLE #results
  15. END
  16. CREATE TABLE #results(
  17. ident INT IDENTITY(1,1) PRIMARY KEY, --Will be used to update the owner.
  18. table_name NVARCHAR(256),
  19. owner_name NVARCHAR(256),
  20. table_rows INT,
  21. reserved_space NVARCHAR(55),
  22. data_space NVARCHAR(55),
  23. index_space NVARCHAR(55),
  24. unused_space NVARCHAR(55))
  25. --Loop through statistics for each table.
  26. INSERT @table(owner_name, table_name)
  27. SELECT
  28. su.name,
  29. so.name
  30. FROM
  31. sysobjects so
  32. INNER JOIN sysusers su ON so.uid = su.uid
  33. WHERE
  34. so.xtype = 'U'
  35. SELECT
  36. @min = 1,
  37. @max = (SELECT MAX(ident) FROM @table)
  38. WHILE @min <= @max
  39. BEGIN
  40. SELECT
  41. @owner = owner_name,
  42. @table_name = table_name
  43. FROM
  44. @table
  45. WHERE
  46. ident = @min
  47. SELECT @sql = 'EXEC sp_spaceused ''[' + @owner + '].[' + @table_name + ']'''
  48. INSERT #results(table_name, table_rows, reserved_space, data_space, index_space, unused_space)
  49. EXEC (@sql)
  50. UPDATE #results
  51. SET owner_name = @owner
  52. WHERE ident = (SELECT MAX(ident) FROM #results)
  53. SELECT @min = @min + 1
  54. END
  55. SELECT * FROM #results order by table_rows
The output of the above query is shown in the following image. We can see that the solution has size and row info.
SQL Server Table size
That's it. I hope it will help someone learning database management.