|
|
|
Scripts to automatically get notified if a database grew due to the autogrowth option being turned on. |
Click here to copy the following block |
DECLARE @FreeAlertNumber int, @SQLString nvarchar(200)
SELECT @FreeAlertNumber = (ISNULL(((SELECT MAX(error) FROM master..sysmessages WHERE error > 50000) + 1), 50001))
EXEC sp_configure N'allow updates', 1 RECONFIGURE WITH OVERRIDE
SELECT @SQLString = 'EXEC sp_addmessage ' + CAST(@FreeAlertNumber AS nvarchar(11)) + ', 10, ' + '''Database %d grew with %s MB.''' + ', ' + '''English''' + ', ' + '''true''' EXEC sp_executesql @SQLString
EXEC sp_configure N'allow updates', 0 RECONFIGURE WITH OVERRIDE go
Use msdb go
CREATE TABLE [dbo].[tbl_DatabaseSizes]( [dbid] [smallint] NOT NULL , [fileid] [smallint] NOT NULL , [filesize] [int] NOT NULL ) go
ALTER TABLE [dbo].[tbl_DatabaseSizes] WITH NOCHECK ADD CONSTRAINT [PK_tbl_DatabaseSizes] PRIMARY KEY CLUSTERED ( [dbid], [fileid] ) GO
USE msdb go
CREATE PROCEDURE usp_FillDatabaseSizeTable AS
SET NOCOUNT ON
DELETE FROM msdb..tbl_DatabaseSizes
DECLARE @db_name varchar(255), @SQLString nvarchar(4000)
DECLARE cur_Databasenames CURSOR FOR
SELECT name FROM master..sysdatabases ORDER BY dbid
OPEN cur_Databasenames FETCH NEXT FROM cur_Databasenames INTO @db_name WHILE @@FETCH_STATUS = 0 BEGIN SELECT @SQLString = 'DECLARE cur_FileIDs CURSOR FOR
SELECT fileid, size FROM ' + @db_name + '..sysfiles
DECLARE @fileid smallint, @filesize int
OPEN cur_FileIDs FETCH NEXT FROM cur_FileIDs INTO @fileid, @filesize
WHILE @@FETCH_STATUS = 0 BEGIN INSERT INTO msdb..tbl_DatabaseSizes SELECT db_id(''' + @db_name + '''), @fileid, @filesize FETCH NEXT FROM cur_FileIDs INTO @fileid, @filesize END CLOSE cur_FileIDs DEALLOCATE cur_FileIDs'
EXEC sp_executesql @SQLString
FETCH NEXT FROM cur_Databasenames INTO @db_name END
Close cur_Databasenames DEALLOCATE cur_Databasenames
SET NOCOUNT OFF
go
USE msdb go
CREATE PROCEDURE usp_CheckFileGrowth AS
SET NOCOUNT ON
DECLARE @db_name varchar(255), @size int, @sizediff int, @SQLString nvarchar(4000)
DECLARE cur_Databasenames CURSOR FOR
SELECT name FROM master..sysdatabases ORDER BY dbid
OPEN cur_Databasenames
DECLARE @AlertNumber int SELECT @AlertNumber = (SELECT error FROM master..sysmessages WHERE description = 'Database %s grew with %d MB.')
FETCH NEXT FROM cur_Databasenames INTO @db_name WHILE @@FETCH_STATUS = 0 BEGIN SELECT @SQLString = 'DECLARE cur_FileIDs CURSOR FOR
SELECT fileid, size FROM ' + @db_name + '..sysfiles
DECLARE @fileid smallint, @filesize int, @filesize_old int, @sizediff int
OPEN cur_FileIDs FETCH NEXT FROM cur_FileIDs INTO @fileid, @filesize
WHILE @@FETCH_STATUS = 0 BEGIN SELECT @filesize_old = (SELECT filesize FROM msdb..tbl_DatabaseSizes WHERE dbid = db_id(''' + @db_name + ''') AND fileid = @fileid)
SELECT @sizediff = CAST(((@filesize - @filesize_old) * 8) / 1024 As Varchar)
IF @filesize_old <> @filesize BEGIN RAISERROR (' + CAST(@Alertnumber As varchar)+ ',10,1, ''' + @db_name + ''',@sizediff) UPDATE msdb..tbl_DatabaseSizes SET filesize = (filesize + (@filesize - @filesize_old)) WHERE dbid = db_id(''' + @db_name + ''') AND fileid = @fileid END FETCH NEXT FROM cur_FileIDs INTO @fileid, @filesize END CLOSE cur_FileIDs DEALLOCATE cur_FileIDs' EXEC sp_executesql @SQLString FETCH NEXT FROM cur_Databasenames INTO @db_name END
Close cur_Databasenames DEALLOCATE cur_Databasenames
EXEC usp_FillDatabaseSizeTable
SET NOCOUNT OFF go
BEGIN TRANSACTION DECLARE @JobID BINARY(16) DECLARE @ReturnCode INT SELECT @ReturnCode = 0 IF (SELECT COUNT(*) FROM msdb.dbo.syscategories WHERE name = N'Database Maintenance') < 1 EXECUTE msdb.dbo.sp_add_category @name = N'Database Maintenance'
SELECT @JobID = job_id FROM msdb.dbo.sysjobs WHERE (name = N'Check Database Sizes') IF (@JobID IS NOT NULL) BEGIN IF (EXISTS (SELECT * FROM msdb.dbo.sysjobservers WHERE (job_id = @JobID) AND (server_id <> 0))) BEGIN RAISERROR (N'Unable to import job ''Check Database Sizes'' since there is already a multi-server job with this name.', 16, 1) GOTO QuitWithRollback END ELSE EXECUTE msdb.dbo.sp_delete_job @job_name = N'Check Database Sizes' SELECT @JobID = NULL END
BEGIN
EXECUTE @ReturnCode = msdb.dbo.sp_add_job @job_id = @JobID OUTPUT , @job_name = N'Check Database Sizes', @owner_login_name = N'sa' , @description = N'No description available.', @category_name = N'Database Maintenance' , @enabled = 1, @notify_level_email = 0, @notify_level_page = 0 , @notify_level_netsend = 0, @notify_level_eventlog = 2, @delete_level= 0 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXECUTE @ReturnCode = msdb.dbo.sp_add_jobstep @job_id = @JobID , @step_id = 1, @step_name = N'Run usp_CheckFileGrowth' , @command = N'EXEC usp_CheckFileGrowth', @database_name = N'msdb' , @server = N'', @database_user_name = N'', @subsystem = N'TSQL' , @cmdexec_success_code = 0, @flags = 0, @retry_attempts = 0 , @retry_interval = 1, @output_file_name = N'', @on_success_step_id = 0 , @on_success_action = 1, @on_fail_step_id = 0, @on_fail_action = 2 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXECUTE @ReturnCode = msdb.dbo.sp_update_job @job_id = @JobID, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXECUTE @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id = @JobID , @name = N'Check for autogrowth daily', @enabled = 1 , @freq_type = 4, @active_start_date = 20021114, @active_start_time = 0 , @freq_interval = 1, @freq_subday_type = 1, @freq_subday_interval = 0 , @freq_relative_interval = 0, @freq_recurrence_factor = 0 , @active_end_date = 99991231, @active_end_time = 235959 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXECUTE @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @JobID, @server_name = N'(local)' IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
END COMMIT TRANSACTION GOTO EndSave QuitWithRollback: IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION EndSave: |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|