Count of Objects by Database on a Server

  • Comments posted to this topic are about the item Count of Objects by Database on a Server

  • 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

  • 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

  • Thanks David! That serves the same purpose in a cleaner, more efficient manner.

  • 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