July 25, 2012 at 3:56 pm
Comments posted to this topic are about the item Count of Objects by Database on a Server
August 1, 2012 at 5:24 am
There should be little change in the script
DECLARE @databases as table (DbName nvarchar(100), DbSize bigint, Remarks nvarchar(100))
DECLARE @results as table (DbName nvarchar(100), DbSize bigint, ObjectCount bigint)
DECLARE @dbname as nvarchar(100),
@dbsize as bigint,
@remarks as nvarchar(100),
@sql as nvarchar(max)
INSERT INTO @databases EXEC sp_databases
SELECT TOP 1 @dbname = DBName, @dbsize = DBSize FROM @databases
WHILE EXISTS (SELECT TOP 1 * FROM @databases)
BEGIN
SET @sql =
' SELECT ''' + @dbname + ''', ''' +
CAST(@dbsize as nvarchar) + ''', COUNT(1) FROM [' + @dbname +
'].[sys].[objects]'
PRINT @sql
INSERT INTO @results EXEC(@SQL)
DELETE FROM @databases WHERE dbname = @dbname
SELECT TOP 1 @dbname = DBName, @dbsize = DBSize FROM @databases
END
SELECT * FROM @results ORDER BY ObjectCount desc
Thanks
Abhilash
August 7, 2012 at 9:06 am
My first thought was to try to use sp_foreachdb
CREATE TABLE #databases(DbName SYSNAME, DbSize BIGINT, Remarks NVARCHAR(100))
INSERT INTO #databases EXEC SP_DATABASES
ALTER TABLE #databases ADD ObjectCount INT
EXEC SP_MSFOREACHDB
'USE [?] ;
UPDATE #databases SET ObjectCount = (SELECT COUNT(*) FROM [sys].[objects])
WHERE DBName = "?"'
SELECT * FROM #databases ORDER BY ObjectCount DESC
DROP TABLE #databases
Regards,
David
August 7, 2012 at 9:21 am
Thanks David! That serves the same purpose in a cleaner, more efficient manner.
May 10, 2016 at 6:45 am
Thanks for the script.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply