You are on page 1of 15

V New in SQL Server 2005, Event Notifications provide

a flexible and powerful way of capturing and handling


events that occur inside of SQL Server.
V Event Notifications execute for DDL Events and
Trace Events inside of the database engine and can
be used instead of DDL triggers or SQL Trace to
capture information about the event being fired.
However, unlike DDL triggers and SQL Trace which
process synchronously, Event Notifications utilize
Service Broker to asynchronously capture event
information.
ë Service Broker is a system for sending and
receiving messages that are typed and contain
information or data.
ë Service Broker also first debuted in SQL Server
2005.
ë In order to make use of Event Notifications, the
first requirement is that Service Broker be enabled
on the database that will contain the broker
components
ë By default msdb has Service Broker enabled on it.
ë Event Notifications utilize a Service Broker Queue
for holding messages generated by firing events.
The queue is similar to a database table that holds
a row for each message that has been added to
the queue. To create a queue to hold the
messages the CREATE QUEUE
DDL command is used:
ë -- Create a service broker queue to hold the
events
CREATE QUEUE EventNotificationQueue
V first we need a queue
V CREATE QUEUE queue evtDdlNotif WITH STATUS
= ON
V then we can create the service
V CREATE SERVICE evtDdlService ON QUEUE
evtDdlNotif
V this is a MS supplied contract
V which uses an existing message type
V {http://schemas.microsoft.com/SQL/Notifications}Eve
ntNotification
(http://schemas.microsoft.com/SQL/Notifications/Pos
tEventNotification
ë 'or event notifications,
ë Microsoft has a predefined message type,
{http://schemas.microsoft.com/SQL/Notifications}E
ventNotification,
ë and a contract,
http://schemas.microsoft.com/SQL/Notifications/P
ostEventNotification.
ë Event notifications differ from triggers by the fact
that the actual notification does not execute any
code. Instead, information about the event is
posted to a SQL Server Service Broker (SSB)
service and is placed on a message queue from
where it can be read by some other process.

ë Another difference between triggers and event


notifications is that the event notifications execute
in response to not only DDL and DML statements
but also some trace events.
ë An event notification involves creation of a target service
to the event notification and the process of creating the
event notification.

=   
  
ë . Create a queue to receive messages.
2. Create a service on the queue that references the
event notifications contract.
3. Create a route on the service to define the address to
which Service Broker sends messages for the service. If
the event notification targets a service in the same
database, the DBA must specify ADDRESS = 'LOCAL'.
ë CREATE QUEUE ExforsysQueue ;
GO
CREATE SERVICE ExforsysService
ON QUEUE ExforsysQueue
(
[http://schemas.microsoft.com/SQL/Notifications/Post
EventNotification]
);
GO
CREATE ROUTE ExforsysRoute
WITH SERVICE_NAME = 'ExforsysService',
ADDRESS = 'LOCAL';
GO
V To create an event notification that is scoped to the
database (ON DATABASE), requires CREATE
DATABASE DDL EVENT NOTI'ICATION permission
in the current database.
V To create an event notification on a DDL statement
that is scoped to the server (ON SERVER), requires
CREATE DDL EVENT NOTI'ICATION permission
in the server.
V To create an event notification on a trace event,
requires CREATE TRACE EVENT NOTI'ICATION
permission in the server.
V To create an event notification that is scoped to a
queue, requires ALTER
permission on the queue.
ë --Create a queue to receive messages.
ë CREATE QUEUE NotifyQueue ;
ë GO
ë --Create a service on the queue that references
ë --the event notifications contract.
ë CREATE SERVICE NotifyService
ë ON QUEUE NotifyQueue
ë ([http://schemas.microsoft.com/SQL/Notifications/PostEventNotification]
);
ë GO
ë --Create a route on the service to define the address
ë --to which Service Broker sends messages for the service.
ë CREATE ROUTE NotifyRoute
ë WITH SERVICE_NAME = 'NotifyService',
ë ADDRESS = 'LOCAL';
ë GO
ë --Create the event notification.
CREATE EVENT NOTI'ICATION log_ddl
ON SERVER
'OR Object_Created
TO SERVICE 'NotifyService',
'8 0a77-3c b- 79-8ac0-8008ab798 ' ;
ë CREATE EVENT NOTI'ICATION
event_notification_name ON { SERVER |
DATABASE | [ ENABLED | DISABLED ] { 'OR {
event_type | DDL_DATABASE_LEVEL_EVENTS }
[ ,...n ] TO broker_service
ë event_notification_nameThis is the name of the
event notification.
ë SERVERThe scope of the event notification is
the current server.
ë DATABASEThe scope of the event notification
is the current database.
ë ENABLEDThis specifies that the event
notification is active when the CREATE statement
has executed.
ë DISABLEDThis specifies that the event notification
is inactive until the notification is activated by
executing an ALTER EVENT NOTI'ICATION
statement.
ë event_typeThis is the name of an event that, after
execution, causes the event notification to execute.
SQL Server Books Online has the full list of events
included in event_type.
ë DDL_DATABASE_LEVEL_EVENTSThe event
notification fires after any of the CREATE, ALTER, or
DROP statements that can be indicated in
event_type execute.
ë broker_serviceThis is the SSB service to which
SQL Server posts the data about an event.

You might also like