Make SQL Server to talk you! Antonios Chatzipavlis Software Architect, Development Evangelist, IT...

14
Make SQL Server to talk you! Antonios Chatzipavlis Software Architect , Development Evangelist, IT Consultant MCT, MCITP, MCPD, MCSD, MCDBA, MCSA, MCTS, MCAD, MCP, OCA MVP on SQL SERVER

Transcript of Make SQL Server to talk you! Antonios Chatzipavlis Software Architect, Development Evangelist, IT...

Page 1: Make SQL Server to talk you! Antonios Chatzipavlis Software Architect, Development Evangelist, IT Consultant MCT, MCITP, MCPD, MCSD, MCDBA, MCSA, MCTS,

Make SQL Server to talk you!Antonios ChatzipavlisSoftware Architect , Development Evangelist, IT ConsultantMCT, MCITP, MCPD, MCSD, MCDBA, MCSA, MCTS, MCAD, MCP, OCAMVP on SQL SERVER

Page 2: Make SQL Server to talk you! Antonios Chatzipavlis Software Architect, Development Evangelist, IT Consultant MCT, MCITP, MCPD, MCSD, MCDBA, MCSA, MCTS,

2

Objectives• SQL Server Agent Alert System• Event Notifications

Page 3: Make SQL Server to talk you! Antonios Chatzipavlis Software Architect, Development Evangelist, IT Consultant MCT, MCITP, MCPD, MCSD, MCDBA, MCSA, MCTS,

3

What Is an Alert?

• Predefined response to an event• Triggered by jobs, performance conditions, or

application log events • Can notify an operator or start a specified job

Page 4: Make SQL Server to talk you! Antonios Chatzipavlis Software Architect, Development Evangelist, IT Consultant MCT, MCITP, MCPD, MCSD, MCDBA, MCSA, MCTS,

4

Create an Alert

Specify

Alert name

Condition (Event, WMI trigger, performance condition) that triggers the alert

Response – notify an operator, start a job

Use SQL Server Management Studio or “sp_add_alert”

EXEC dbo.sp_add_alert @name = N'Test Alert', @message_id = 55001, @severity = 0,

@notification_message = N'Error 55001 has occurred. The database will be backed up...',

@job_name = N'Back up the AdventureWorks Database' ;

Page 5: Make SQL Server to talk you! Antonios Chatzipavlis Software Architect, Development Evangelist, IT Consultant MCT, MCITP, MCPD, MCSD, MCDBA, MCSA, MCTS,

5

Responding to Alerts

Launch a Job

Use an existing job

Create a new job

Notify an Operator

Schedule responders for different parts of the day or week

E-mail, pager, or net send

Either

Or

Page 6: Make SQL Server to talk you! Antonios Chatzipavlis Software Architect, Development Evangelist, IT Consultant MCT, MCITP, MCPD, MCSD, MCDBA, MCSA, MCTS,

6

DEMOUsing Alerts

Page 7: Make SQL Server to talk you! Antonios Chatzipavlis Software Architect, Development Evangelist, IT Consultant MCT, MCITP, MCPD, MCSD, MCDBA, MCSA, MCTS,

7

Overview of Event Notifications• Messages containing event data

DDL EventsTrace EventsDML EventsDatabase and Server Events

• Sent to an event processing service by usingService Broker

A message type and contract are predefinedYou must create a queue, a service, and a route

Page 8: Make SQL Server to talk you! Antonios Chatzipavlis Software Architect, Development Evangelist, IT Consultant MCT, MCITP, MCPD, MCSD, MCDBA, MCSA, MCTS,

8

Creating Event Notifications

1. Define the Event Notification2. Specify the Scope3. Specify the Event4. Specify the Service

CREATE EVENT NOTIFICATION NotifyALTER_T1 ON DATABASE FOR ALTER_TABLE TO SERVICE '//Adventure-Works.com/ArchiveService' , '8140a771-3c4b-4479-8ac0-81008ab17984';

Page 9: Make SQL Server to talk you! Antonios Chatzipavlis Software Architect, Development Evangelist, IT Consultant MCT, MCITP, MCPD, MCSD, MCDBA, MCSA, MCTS,

9

Processing Event Notifications

1. Receive the message2. Extract event data by using XQuery

DECLARE @messageTypeName NVARCHAR(256),@messageBody XML

;RECEIVE TOP(1) @messageTypeName = message_type_name,@messageBody = message_bodyFROM dbo.NotifyQueue;

IF @@ROWCOUNT = 0RETURN

DECLARE @cmd nvarchar(1000)DECLARE @posttime nvarchar(24)DECLARE @spid nvarchar(6)

SET @cmd = @messagebody.value('(/EVENT_INSTANCE/TSQLCommand/CommandText)

[1]','nvarchar(100)')

SET @messagebody.value('(/EVENT_INSTANCE/PostTime)

[1]','nvarchar(24)')SET @spid = @messagebody.value

('(/EVENT_INSTANCE/SPID)[1]','nvarchar(6)')

Page 10: Make SQL Server to talk you! Antonios Chatzipavlis Software Architect, Development Evangelist, IT Consultant MCT, MCITP, MCPD, MCSD, MCDBA, MCSA, MCTS,

10

Managing Event Notifications

1. Viewing event notifications and queues2. Deleting event notifications

SELECT name FROM sys.event_notificationsSELECT definition FROM sys.service_queues

DROP EVENT NOTIFICATION UpdateStats ON SERVER

Page 11: Make SQL Server to talk you! Antonios Chatzipavlis Software Architect, Development Evangelist, IT Consultant MCT, MCITP, MCPD, MCSD, MCDBA, MCSA, MCTS,

11

DEMOImplementing Event Notifications

Page 12: Make SQL Server to talk you! Antonios Chatzipavlis Software Architect, Development Evangelist, IT Consultant MCT, MCITP, MCPD, MCSD, MCDBA, MCSA, MCTS,

12

Q & A

Page 13: Make SQL Server to talk you! Antonios Chatzipavlis Software Architect, Development Evangelist, IT Consultant MCT, MCITP, MCPD, MCSD, MCDBA, MCSA, MCTS,

13

My Blogs

http://www.autoexec.gr/blogs/antonch

http://www.dotnetzone.gr/cs/blogs/antonch

Page 14: Make SQL Server to talk you! Antonios Chatzipavlis Software Architect, Development Evangelist, IT Consultant MCT, MCITP, MCPD, MCSD, MCDBA, MCSA, MCTS,

14

Thank you