Monday, April 8, 2013

Get all tables information in the database


Some times we may think to see single table information mainly the row count and will get the result easily by typing sp_spaceused tableName.
But how to get all the tables information within the database. Please have this script.
DECLARE @SpaceUsed TABLE
(
 TableName VARCHAR(100)
 ,No_Of_Rows BIGINT
 ,ReservedSpace VARCHAR(15)
 ,DataSpace VARCHAR(15)
 ,Index_Size VARCHAR(15)
 ,UnUsed_Space VARCHAR(15)
 )
DECLARE @sqlstring VARCHAR(500)
SET @sqlstring  =  'exec sp_spaceused ''?'''
INSERT INTO @SpaceUsed EXEC sp_msforeachtable @command1=@sqlstring
SELECT * FROM @SpaceUsed
Of course, you could use the following too. But always depends on the format you want the cost to execute.
DECLARE @sqlstring VARCHAR(500)
 SET @sqlstring = 'exec sp_spaceused ''?'''
 EXEC sp_msforeachtable @command1=@sqlstring

No comments:

Post a Comment