This post is for Julian...One of my dear trouble makers! :)))
Junlian came to DBA team to ask for a report of table size of all tables on all databases. My instant thought was to use the standard report of disk usage by table. However, no no no, he wanted more. The report only provides the table size for each database and doesn’t list all databases’ table sizes. And then I tried to combine sp_MSforeachdb and sp_MSforeachtable mentioned in the previous post to check all the tables on all the databases. Bad news! They don't work together well. OK, more work, and lets write a script from scratch then. How exciting!
Finally I came up with this solution.
Junlian came to DBA team to ask for a report of table size of all tables on all databases. My instant thought was to use the standard report of disk usage by table. However, no no no, he wanted more. The report only provides the table size for each database and doesn’t list all databases’ table sizes. And then I tried to combine sp_MSforeachdb and sp_MSforeachtable mentioned in the previous post to check all the tables on all the databases. Bad news! They don't work together well. OK, more work, and lets write a script from scratch then. How exciting!
Finally I came up with this solution.
IF Object_id('tempdb..#DatabaseTables') IS NOT NULL
DROP TABLE #databasetables;
CREATE TABLE #databasetables
(
id INT IDENTITY(1, 1),
[dbname] SYSNAME,
tablename NVARCHAR(200)
)
IF Object_id('tempdb..#TableSizeStaging') IS NOT NULL
DROP TABLE #TableSizeStaging;
CREATE TABLE #TableSizeStaging
(
tablename NVARCHAR(200),
rows CHAR(11),
reserved VARCHAR(18),
data VARCHAR(18),
index_size VARCHAR(18),
unused VARCHAR(18)
)