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.
- All the column names must be explicitly defined in a SQL query (do not use (*) to retrieve the columns of the database) as in the following:
SELECT Col1, Col2, Col3 FROM dbo.Table1
- The SQL query does not contain unnamed or duplicate columns.
- The Query table must have a schema name
- The SQL query must not reference a table with a computed column.
- The SQL Query must not contain PIVOT or UNPIVOT operators.
- The SQL Query must not contain UNION, INTERSECT, EXCEPT, DISTINCT, COMPUTE or COMPUTE BY, or INTO operators.
- The SQL must not reference to the view.
- The SQL Query must not reference a derived table, table variable or temporary table.
- The SQL query must not contain sub query, outer joins or self-join.
- The SQL query must not reference the large object such as text, ntext or image.
- The SQL query must not use any full text predicates such as CONTAINS and FREETEXT.
- The SQL query must not use any aggregate function such as AVG, COUNT, MAX, MIN and so on.
- The SQL query must not reference any server global variables.
- The SQL query must not use a table or a view from another database or server.
- The SQL Query must not use the TOP expression.
- The SQL query must not reference any Service broker Queue.
- The SQL query must not contain any rowset functions such as OPENROWSET and OPENQUERY.
- The SQL query must not use any user defined aggregates.
- The SQL query must not use any system tables or views.
- The Query must not run under READ_UNCOMMITTED or SNAPSHOT isolation levels.
- The Option Setting and isolation level also plays an important role in Query Notification. When a SQL SELECT query is executed under the Query Notification, the request must be under ANSI_NULLS ON, ANSI_PADDING ON, ANSI_WARNINGS ON, CONCAT_NULL_YIELDS_NULL ON, QUOTED_IDENTIFIER ON, NUMERIC_ROUNDABORT OFF, and ARITHABORT ON Options.
If the options and isolation level are not set appropriately then notification is fired immediately after the SELECT statement is executed. - If the Stored Procedure is used as a notification query then the ANSI_NULLS and the QUOTED_IDENTIFIER option must be set when the Stored Procedure is created.


Anupam SinghPosted Jun 12, 2014, 12:59 AM
Nice article.