Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help with getting a Table Count across multiple DBs


Help with getting a Table Count across multiple DBs

Author
Message
don 57678
don 57678
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: 9
Hello,

I'm not all that familiar with SQL scripting, I can usually figure stuff out, however I'm stuck on a project at work and have been searching for an answer with no luck. It may not ever be possible.

We have a SQL Server 2008 with approx 500 client DB's on them There is a specific Table called "CurrentUsers". What I'm trying to achieve is, I need to run this query on that table (Select Count (*) from CurrentUsers), which tells me how many users our on the application. The part here is I need to run this against ALL DB's (not including the system dbs), and have it give me a Sum of ALL CurrentUsers from all the DB's.

Is this even possible?

Thanks a bunch!!!

Don
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8487 Visits: 18093
Here's a code to show you how can it be done. You might need to fix it.
DECLARE @dbname sysname

CREATE TABLE #counts( totalcounts int)

DECLARE DBs CURSOR FOR
SELECT name
FROM sys.databases
WHERE database_id > 4
AND state = 0

OPEN DBs
FETCH NEXT FROM DBs INTO @dbname

WHILE @@FETCH_STATUS = 0
BEGIN
   EXECUTE( 'INSERT INTO #counts SELECT COUNT(*) FROM [' + @dbname + '].sys.tables' )
   
   FETCH NEXT FROM DBs INTO @dbname
END
CLOSE DBs
DEALLOCATE DBs

SELECT SUM(totalcounts)
FROM #counts

DROP TABLE #counts




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
don 57678
don 57678
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: 9
I don't know how you guys know this stuff off the top of you head like this... Worked like a charm!!!! You just saved me a ton of time! Thank you Thank you Thanks you!!!!!!!
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14933 Visits: 38926
my version is just an adaptation of Luis's: i'm just selecting the rows form the indexes, as the count is already materialized, and is faster on huge tables.

Thank you Luis for 99% of the code!


DECLARE @cmd VARCHAR(MAX),
@dbname VARCHAR(128)

CREATE TABLE #counts( DatabaseName VARCHAR(128),totalcounts INT)

DECLARE DBs CURSOR FOR
SELECT name
FROM sys.databases
WHERE database_id > 4
AND STATE = 0

OPEN DBs
FETCH NEXT FROM DBs INTO @dbname

WHILE @@FETCH_STATUS = 0
BEGIN

SET @cmd = '

INSERT INTO #counts(DatabaseName,totalcounts)
SELECT
''' + @dbname + ''' AS DatabaseName,
SUM(ROWS) AS NumRows
FROM ' + QUOTENAME(@dbname) + '.sys.partitions p
INNER JOIN ' + QUOTENAME(@dbname) + '.sys.sysobjects o
ON p.object_id = o.id
WHERE p.index_id IN (0,1)
AND o.type = ''U''
AND o.name = ''CurrentUsers''
GROUP BY p.OBJECT_ID,p.index_id '
PRINT @cmd
EXEC(@cmd)
FETCH NEXT FROM DBs INTO @dbname
END
CLOSE DBs
DEALLOCATE DBs

SELECT * FROM #counts
SELECT SUM(totalcounts) FROM #counts

drop table #counts






Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

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