Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Determine table row count and sizes of full database
-- Determine table row count and sizes of full database
-- Inspired from http://stackoverflow.com/a/19916574/25182
SET NOCOUNT ON
DECLARE @TableInfo TABLE (tablename varchar(128), [rows] int, reserved varchar(18), [data] varchar(18), index_size varchar(18), unused varchar(18))
DECLARE @cmd1 varchar(200)
SET @cmd1 = 'exec sp_spaceused ''?'''
INSERT INTO @TableInfo (tablename,[rows],reserved,[data],index_size,unused)
EXEC sp_msforeachtable @command1=@cmd1
SELECT tablename
, [rows]
, Convert(int,Replace([reserved],' KB','')) AS [reservedKB]
, Convert(int,Replace([data],' KB','')) AS [dataKB]
, Convert(int,Replace([index_size],' KB','')) AS [index_sizeKB]
, Convert(int,Replace([unused],' KB','')) AS [unusedKB]
FROM @TableInfo ORDER BY tablename
EXEC sp_spaceused
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment