Skip to content

Instantly share code, notes, and snippets.

@bvanskiver
Last active January 23, 2023 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bvanskiver/863cb80bb728b4feb2b2 to your computer and use it in GitHub Desktop.
Save bvanskiver/863cb80bb728b4feb2b2 to your computer and use it in GitHub Desktop.
Space used by tables
declare @SpaceUsed table (TableName sysname, NumRows bigint, ReservedSpace varchar(50), DataSpace varchar(50), IndexSize varchar(50), UnusedSpace varchar(50))
declare @str varchar(500)
set @str = 'exec sp_spaceused ''?'''
insert into @SpaceUsed
exec sp_msforeachtable @command1=@str
select TableName,
FORMAT(CAST(NumRows as bigint), 'N0') as NumRows,
FORMAT(CAST(REPLACE(ReservedSpace, ' KB', '') as bigint), 'N0') + ' KB' as ReservedSpace,
FORMAT(CAST(REPLACE(DataSpace, ' KB', '') as bigint), 'N0') + ' KB' as DataSpace,
FORMAT(CAST(REPLACE(IndexSize, ' KB', '') as bigint), 'N0') + ' KB' as IndexSize,
FORMAT(CAST(REPLACE(UnusedSpace, ' KB', '') as bigint), 'N0') + ' KB' as UnusedSpace
from @SpaceUsed
--order by TableName
order by CAST(REPLACE(DataSpace, ' KB', '') as bigint) desc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment