Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
WJHamel
Aged Yak Warrior
651 Posts |
Posted - 2011-11-21 : 14:27:17
|
| We have the following sproc that is in our Master db:USE [SIZING_DB]GO/****** Object: StoredProcedure [dbo].[GETSTATS] Script Date: 11/21/2011 13:07:07 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[GETSTATS]WITH EXECUTE AS CALLERASCREATE TABLE #t ( [name] NVARCHAR(128),[rows] CHAR(11),reserved VARCHAR(18), data VARCHAR(18), index_size VARCHAR(18),unused VARCHAR(18)) INSERT #t EXEC sp_msForEachTable 'EXEC sp_spaceused ''?''' SELECT DB_NAME() as DBName, GETDATE() as DateCreated, *into SIZING_DB.dbo.DBTableStatsFROM #t-- # of rows.SELECT DB_NAME() as DBName, GETDATE() as DateCreated, SUM(CAST([rows] AS int)) AS [rows]INTO SIZING_DB.dbo.RowCountsFROM #tDROP TABLE #tINSERT INTO sizing_db.dbo.dbstatsSELECT GETDATE() AS date_and_time,DB_NAME() AS database_name,SUM(total_pages) AS reservedKB,SUM(used_pages) AS usedKB,SUM(total_pages -used_pages) AS unallocatedKBFROM sys.allocation_unitsINSERT INTO sizing_db.dbo.dbstats2 SELECT GETDATE() AS date_and_time,DF.name,SIZE,FILEPROPERTY(DF.name, 'spaceused') AS pages_used,FILEPROPERTY(DF.name, 'spaceused') * 1.0 / SIZE * 100 AS pct_usedFROM sys.database_files AS DFSELECT dbstats.uniquekey,dbstats.date_and_time,dbstats.database_name,dbstats.reservedKB,dbstats.usedKB,dbstats.unallocatedKB,dbstats2.size,dbstats2.pages_used,dbstats2.pct_usedFROM sizing_db.dbo.dbstats dbstatsINNER JOINsizing_db.dbo.dbstats2 dbstats2ON (dbstats.uniquekey = dbstats2.uniquekey)SELECT * FROM SIZING_DB.dbo.DBTableStatsDROP TABLE sizing_db.dbo.DBTableStatsWe are 'supposed' to be able to execute this sproc from that location have it provide the data against the other db's on the server. What is the syntax for firing this off correctly? I know it is 'EXEC sp_getstats', followed by something related to one of the other dbs (i.e., Arrest, MNI, EVIDENCE, CIVIL, etc.)thanks |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
WJHamel
Aged Yak Warrior
651 Posts |
Posted - 2011-11-21 : 15:08:56
|
| I'm still looking at your example and will admit it's going to take me a while to digest it. In the meantime, i've noticed that if i run this sproc from within the master database, using, for example:EXEC sp_GETSTATS USE EvidenceIt will provide me with the result sets i'm looking for, but each time i execute it with a different USE statement, the result set is one exec cycle behind. For instance, if i were to follow the above example with EXEC sp_GETSTATS use CIVIL, it will provide me with the stats for the EVIDENCE db. If i execute the statement with the USE CIVIL suffix a second time, it provides me with the stats for CIVIL. Any ideas why? |
 |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2011-11-21 : 16:22:10
|
| Because that's actually 2 statements:EXEC sp_GETSTATS; use CIVIL;The exec runs and then the USE changes the database context. Hence, next time you run, the database context is Civil.--Gail ShawSQL Server MVP |
 |
|
|
|
|
|
|
|