SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Count of Objects by Database on a Server


Count of Objects by Database on a Server

Author
Message
jkennedy 33173
jkennedy 33173
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 38
Comments posted to this topic are about the item Count of Objects by Database on a Server
Abhilash T
Abhilash T
Valued Member
Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)

Group: General Forum Members
Points: 52 Visits: 182
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
david.ashby
david.ashby
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 48
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
jkennedy 33173
jkennedy 33173
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 38
Thanks David! That serves the same purpose in a cleaner, more efficient manner.
Iwas Bornready
Iwas Bornready
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16044 Visits: 885
Thanks for the script.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search