Last active
January 23, 2023 12:15
-
-
Save bvanskiver/863cb80bb728b4feb2b2 to your computer and use it in GitHub Desktop.
Space used by tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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