TSQL: How to count the rows, columns, and size of each table in your db

Posted by admin at 29th January, 2009

USE [dbName]
GO
CREATE TABLE #temp (
table_name sysname ,
row_count INT,
reserved_size VARCHAR(50),
data_size VARCHAR(50),
index_size VARCHAR(50),
unused_size VARCHAR(50))
SET NOCOUNT ON
INSERT #temp
EXEC sp_msforeachtable 'sp_spaceused ''?'''
SELECT a.table_name,
a.row_count,
COUNT(*) AS col_count,
a.data_size
FROM #temp a
INNER JOIN information_schema.columns b
ON a.table_name collate database_default
= b.table_name collate database_default
GROUP BY a.table_name, a.row_count, a.data_size
ORDER BY CAST(REPLACE(a.data_size, ' KB', '') AS integer) DESC
DROP TABLE #temp

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
Category : Bills Posts / Programming

You must be logged in to post a comment.


Recent Comments
  • PirateKay: I really hope that it comes back to Fort Wayne next year sin...
  • admin: Woot - I got a response. I didn't think that there wasn't pe...
  • cyclone: You said: "There’s nothing wrong with living in a small to...
  • MrsH: You're so right, Bill! Thomas Jefferson would roll over in ...
  • Escarlata@FaireNews.com: Thanks for sharing how Golden Gate went, Bill. It sounds lik...
Flickr