Introduction

SQL Server 2005 introduced Query Notification. Query Notification allows an application to request a notification from SQL Server when the Query result is changed. Query Notification helps us to reduce round trips to the database. Previously we needed to check the data periodically to maintain query results, using Query Notification the programmer can design such an application that is automatically notified when the query result is changed.

The Database Engine uses a Service Broker to deliver the notification messages and use notification subscriptions to track requests for Query Notification. Query Notification does not use Notification Services and also they are independent of event notifications. The Query Notification feature is very useful for the applications that provide cache data such as web application and need to notify when the data is changed in the database.

The Query Notification functionality is developed on top of the change detection mechanism of the database. To do this database engine, maintain the indexed views, so the requirements and restrictions of Query used in Query Notification are the same as an indexed view.

Some points must be considered while writing SQL Query for Query Notification.

Prerequisites to Create SQL Query Notification Request

  1. Enable Service Broker

    A Service Broker is used to deliver notification message to the query requestor, so the Service Broker must be enabled for the database. Please refer to Service-broker-in-sql-server.

    If Query Notification is a subscription done from an application other than the database, the TRUSTWORTHY property must be set to ON for the database that contains the subscription.
  2. Query Notification requires a few permissions for the database. A notification subscription is owned by the database principal that is used to execute the command that registered the subscription.

    USE DataBaseName
    GRANT SUBSCRIBE QUERY NOTIFICATIONSTO database_principal

    To receive the notification, the subscriber user must have RECEIVE permissions and also SEND permission.

    GRANT RECEIVEON Table TO UserName(login)
    GRANT SENDON SERVICE:://the serviceName to login
  3. SQL Query that is used for Query Notification must fulfill the above mentioned points.

The Query Notification message contains XML. Every notification message contains a date and time of when the notification was created and the reason for the notification. There are the main two attributes in the notification message: The QueryNotification attribute identifies the reason for the message and the Message attribute contains the notification id for the subscription. The Query Notification is type "http://schemas.microsoft.com/SQL/Notifications/QueryNotification" and the Message is part of "http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification".

The following is a sample Query Notification message:

<qn:QueryNotification
xmlns:qn="http://schemas.microsoft.com/SQL/Notifications/QueryNotification"
Type="change" Source="data" Info="insert">
<qn:Message>http://mysite.microsoft.com/catalog.aspx?Category=Cars</qn:Message>
</qn:QueryNotification>

Please refer Query Notification Message for more information.

The dynamic management view sys.dm_qn_subscriptions contains information about the active notification subscription. Using this dynamic management, we can check for an active notification subscription for a specific database on the server.

Query Notification

Summary

Query Notification provides a way to reduce the roundtrip to the database if the data changes within a query vary infrequently. Query Notification is very useful in situations where an application reads the data from a database very frequently but the data changes very infrequently. Query Notifications work fine when the number of notifications are light to moderate and the application does not require an immediate notification response time if the data changes. Query Notification might not be a good choice for the application in which notification is received within a sub second of time or the volume of the notification is very high.