Monday, October 19, 2009
Postings
Sunday, October 18, 2009
Application Alerts!
Tuesday, September 1, 2009
Getting notified of structure changes via DDL Triggers
This procedure notifies our DBA Group of table and view changes by using a Database trigger. The trigger exposes the system function EVENTDATA() which returns XML data. See EVENTDATA in BOL for schema. This parses the XML to get information needed, and then uses the old standby sp_send_dbmail. Once the trigger is created they can be managed via SSMS by the path Database\Programmability\Database Triggers
USE MyDatabase
GO
CREATE TRIGGER Notify_Table_changes_MyDataBase ON DATABASE
FOR CREATE_TABLE, DROP_TABLE, ALTER_TABLE, CREATE_VIEW, ALTER_VIEW,
DROP_VIEW
AS
/*
Can View triggers under Database\Programmability\Database Triggers
*/
DECLARE @data XML
SET @data = EVENTDATA()
DECLARE @Recipients VARCHAR(MAX),
@Body VARCHAR(2500),
@Subject VARCHAR(50)
SET @Recipients = 'DBAGroup@MyCo.com'
SET @Subject = @@SERVERNAME +':'+RTRIM(LTRIM(@data.value('(/EVENT_INSTANCE/ServerName)[1]',
'nvarchar(100)'))) + '-'
+ RTRIM(LTRIM(@data.value('(/EVENT_INSTANCE/DatabaseName)[1]',
'nvarchar(100)'))) + '-' + RTRIM(LTRIM(@data.value('(/EVENT_INSTANCE/EventType)[1]', 'nvarchar(100)')))
+ '-' + RTRIM(LTRIM(@data.value('(/EVENT_INSTANCE/ObjectName)[1]',
'nvarchar(100)')))
SET @Body = 'User:' + CONVERT(NVARCHAR(100), SYSTEM_USER) + ' Ran:'
+ @data.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'nvarchar(2000)')
EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Dataprofile',
@recipients = @Recipients, @body = @Body, @subject = @Subject
Sunday, August 16, 2009
Start Up Procedures for SQL Sever (2005)
Start up procedures execute when the SQL Server Service is started. This can be advantageous at times. First you have to set the “Scan for Startup Procs” via sp_configure. Another, according to BOL is sp_procoption this sets the “Scan for startup Procs” and which one to execute.’ Below is the code for the proc that I want to run DBA_StartUpProc when the SQL Server service is started. After the Procedure code below there is code to run sp_procoption.
The Purpose of DBA_StartUpProc is to notify me when a SQL Server Service starts, if it’s one of our production servers I have it text me and other Operators. Why? There are several reasons: We have a cluster and I want to know when it fails over – a failover basically restart SQL Server on the other node. If a server goes bump in the night and restarts I want to know so I can check the services and jobs running on that server. To verify that something did happen that was supposed to like HW/SW/OS updates that cause a reboot or restart of SQL Server
The main flow of this proc is to determine if it’s a production server, then get then get the email addresses from the operators table in msdb, then it’s just formatting the addresses and setting parameters to call msdb.dbo.sp_send_dbmail
USE [master]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROC [dbo].[DBA_StartUpProc]
AS /*
12/11/2007 Dave Ott
Purpose of this proc is to notify when service is started
production servers will send an email and a text message,
all others will get an email
*/
SET NOCOUNT ON
DECLARE @ProductionServerList VARCHAR(100),
@IsProductionServer BIT,
@recipients VARCHAR(4000),
@email VARCHAR(1000),
@Subject VARCHAR(100),
@Message VARCHAR(100)
DECLARE @emails TABLE ( Email VARCHAR(1000) )
SET @ProductionServerList = 'Server1,Server2,Server3'-- so far
SET @IsProductionServer = 0
-- set is production server flag
IF CHARINDEX(@@SERVERNAME, @ProductionServerList) <> 0
SET @IsProductionServer = 1
-- get list of emails and in the case of production text/Pager emails
INSERT INTO @emails
SELECT CASE WHEN email_address IS NULL THEN ''
ELSE email_address
END
+ -- if production get pager adress too
CASE WHEN @IsProductionServer = 1
AND pager_address IS NOT NULL
THEN CASE WHEN email_address IS NULL THEN pager_address
ELSE ';' + pager_address
END
ELSE ''
END
FROM msdb.dbo.sysoperators WITH ( NOLOCK )
WHERE email_address IS NOT NULL
-- get all Recipants
SET @recipients = ''
WHILE EXISTS ( SELECT TOP 1
*
FROM @emails )
BEGIN
SET @email = ( SELECT MIN(Email)
FROM @emails
)
SET @recipients = @recipients + -- we need a ; between emails
CASE WHEN LEN(@recipients) = 0 THEN @email
ELSE ';' + @email
END
DELETE FROM @emails
WHERE Email = @Email
END
-- send email
SET @Subject = 'SQL Server ' + @@servername + ' was started...'
SET @Message = 'Which means that it was stopped for some reason'
EXEC msdb.dbo.sp_send_dbmail @recipients = @recipients,
@profile_name = 'Profile', @body = @Message, @subject = @Subject,
@importance = 'High'
GO
-- Accpording to books online this also sets 'scan for startup procs' in sp_configure
EXEC sp_procoption N'[dbo].[DBA_StartUpProc]', 'startup', '1'