Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Getting info across several servers
Message
From
14/09/2006 09:12:42
 
 
To
13/09/2006 09:53:20
Keith Payne
Technical Marketing Solutions
Florida, United States
General information
Forum:
Microsoft SQL Server
Category:
Database management
Miscellaneous
Thread ID:
01153070
Message ID:
01153642
Views:
8
>>>>I have put together the procedure included. It is not quite finished yet. Right now it collects the DB size info for all of the databases on a given server and puts the info into a table. It only gets the info for the current server. It would be more effort than it’s worth to put it on servers with only a few production DBs. I would like it to run across a given set of servers, but I have not found a way to do that yet.
>>>>
>>>>Please note that much of the key ideas in this code came from (thanks Sergey):
>>>>http://www.databasejournal.com/features/mssql/article.php/1467771
>>>>
>>>>
>>>>/**************************************************************
>>>>Name: sGetDBMaintInfo
>>>>Description:
>>>>   Gather the data and log space for all databases on the system
>>>>and insert the information into DBMaintInfo. The following databases
>>>>are not added to DBMaintInfo:
>>>>	pubs
>>>>	Northwind
>>>>	model
>>>>	tempdb
>>>>
>>>>Author: Rollin Burr
>>>>**************************************************************/
>>>>CREATE PROCEDURE sGetDBMaintInfo AS
>>>>
>>>>-- IF DBMaintInfo table does not yet exist, create it.
>>>>IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[DBMaintInfo]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
>>>>BEGIN
>>>>	CREATE TABLE [dbo].[DBMaintInfo] (
>>>>		[DatabaseName] [varchar] (80) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
>>>>		[DataSpaceUsed_MB] [real] NULL ,
>>>>		[DataSpaceFree_MB] [real] NULL ,
>>>>		[TLogSpaceUsed_MB] [real] NULL ,
>>>>		[TLogSpaceFree_MB] [real] NULL ,
>>>>		[LastFullBackup] [datetime] NULL ,
>>>>		[LastDiffBackup] [datetime] NULL ,
>>>>		[LastTransBackup] [datetime] NULL ,
>>>>		[EntryDate] [datetime] NOT NULL
>>>>	) ON [PRIMARY]
>>>>
>>>>	ALTER TABLE [dbo].[DBMaintInfo] WITH NOCHECK ADD
>>>>		CONSTRAINT [PK_DBMaintInfo] PRIMARY KEY  CLUSTERED
>>>>		(
>>>>			[DatabaseName],
>>>>			[EntryDate]
>>>>		)  ON [PRIMARY]
>>>>
>>>>	ALTER TABLE [dbo].[DBMaintInfo] ADD
>>>>		CONSTRAINT [DF_DBMaintInfo_EntryDate] DEFAULT (getdate()) FOR [EntryDate]
>>>>END  --IF NOT EXISTS
>>>>
>>>>-- Set up error check
>>>>SET NOCOUNT ON
>>>>DECLARE @err INT
>>>>SET @err = 0
>>>>
>>>>--Create the temp tables to hold the results of DBCC commands until the information is entered into DBMaintInfo.
>>>>CREATE TABLE #logspace (
>>>>	DBName	VARCHAR( 100),
>>>>	LogSize		FLOAT,
>>>>	PrcntUsed	FLOAT,
>>>>	status 		INT
>>>>   )
>>>>
>>>>CREATE TABLE #dataspace (	
>>>>	FileID 		INT,
>>>>	FileGrp 		INT,
>>>>	TotExt		INT,
>>>>	UsdExt 		INT,
>>>>	LFileNm		VARCHAR( 100),
>>>>	PFileNm		VARCHAR( 100)
>>>>   )
>>>>
>>>>--Get the log space
>>>>INSERT INTO #logspace
>>>>   EXEC ('DBCC SQLPERF( LOGSPACE)')
>>>>
>>>>/*
>>>>Get the data space
>>>>Use a cursor to loop through the results from DBCC
>>>>since you have to run this command from each database
>>>>with a USE command.
>>>>*/
>>>>DECLARE
>>>>	@DBName	VARCHAR( 100),
>>>>	@LogSize	FLOAT,
>>>>	@PrcntUsed	FLOAT,
>>>>	@cmd 		VARCHAR( 500),
>>>>	@DBMIID	INT
>>>>DECLARE dbname CURSOR
>>>>FOR SELECT DBName, LogSize, PrcntUsed FROM #logspace
>>>>OPEN dbname
>>>>FETCH NEXT FROM dbname INTO @DBName, @LogSize, @PrcntUsed	
>>>>WHILE @@fetch_status = 0
>>>>BEGIN
>>>>	SELECT @cmd = 'use ' + rtrim( @DBName) + ' dbcc showfilestats'
>>>>	INSERT #dataspace
>>>>	EXEC( @cmd)
>>>>	IF @DBName NOT IN ('model','tempdb','pubs','Northwind')
>>>>	BEGIN
>>>>		INSERT INTO DBMaintInfo (DatabaseName, DataSpaceUsed_MB, DataSpaceFree_MB,
>>>>			TLogSpaceUsed_MB, TLogSpaceFree_MB, EntryDate)
>>>>		SELECT @DBName,
>>>>			((cast( UsdExt AS NUMERIC( 10, 4))* 32) / 512),
>>>>			((cast( TotExt - UsdExt AS NUMERIC( 10, 4))* 32) / 512),
>>>>			@LogSize * (@PrcntUsed/100),
>>>>			@LogSize * ((100-@PrcntUsed)/100),
>>>>			GETDATE()
>>>>		FROM #dataspace
>>>>	END
>>>>
>>>>	FETCH NEXT FROM dbname INTO @DBName, @LogSize, @PrcntUsed
>>>>	DELETE #dataspace
>>>>END
>>>>CLOSE dbname
>>>>DEALLOCATE dbname
>>>>
>>>>-- Drop the temporary tables
>>>>DROP TABLE #logspace
>>>>DROP TABLE #dataspace
>>>>
>>>>-- Remove all data from table that is older than 90 days
>>>>DELETE FROM DBMaintInfo WHERE EntryDate < GETDATE() - 90
>>>>
>>>>RETURN @err
>>>>
>>>>GO
>>>>
>>>
>>>Rollin,
>>>
>>>How about creating a system stored procedure on each server that returns as a result set what would normally get inserted into DBMaintInfo? Then you can insert the results of the stored proc on each server into the real DBMaintInfo table.
>>
>>Thanks for your response Keith.
>>How would I 'insert the results of the stored proc on each server into the real DBMaintInfo table'? The several servers are not linked.
>
>That's a tough one! Maybe use FOR XML in the query and save it to a file on a mapped drive where the 'primary' server can pick it up.

I have been trying to find a way to export (append) temp table data to an external file (say Excel) from inside a procedure. I have not found one. Any ideas??
Thank You

Rollin Burr

Politicians and diapers have one thing in common. They should both be changed regularly, and for the same reason.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform