Skip to content

Instantly share code, notes, and snippets.

@NaserKhoshfetrat
Last active June 10, 2022 10:05
Show Gist options
  • Save NaserKhoshfetrat/2df1beaf4247b918bc0232c5d06372fe to your computer and use it in GitHub Desktop.
Save NaserKhoshfetrat/2df1beaf4247b918bc0232c5d06372fe to your computer and use it in GitHub Desktop.
sql server procedure TableSpaceUsed
--<sys.sp_MSforeachtable>: execute sp_spaceused for all of database table
--<sp_spaceused>: return rows, index size and data size of table
EXEC sys.sp_MSforeachtable 'sp_spaceused ''?''';
--or as table
USE [Fabrics]
GO
CREATE TABLE #TableSpaceUsed(
[name] [nvarchar](120) NULL,
[rows] [nvarchar](120) NULL,
[reserved] [nvarchar](120) NULL,
[data] [nvarchar](120) NULL,
[index_size] [nvarchar](120) NULL,
[unused] [nvarchar](120) NULL
) ON [PRIMARY]
Insert Into #TableSpaceUsed
EXEC sys.sp_MSforeachtable 'sp_spaceused ''?''';
Select * from #TableSpaceUsed
Order by CAST([rows] as int) desc
Drop table #TableSpaceUsed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment