Spent a day chasing down an issue with SQL Service Broker not working, the root cause was referencing the database in the stored procedure.
For example, this select
works fine in SQL Management Studio:
select [MyColumn] from [MyDatabase].[MySchema].[MyTable]
However, this is rejected by SQL Service Broker because we are referencing the database in the select statement, and the callback from SqlDependency
comes back with Invalid
in SqlNotificationEventArgs e
, see http://msdn.microsoft.com/en-us/library/ms189308.aspx.
Altering the SQL passed into SqlDependency to the following statement eliminated the error:
select [MyColumn] from [MySchema].[MyTable]
Update
The example above is just one of many, many limitations to the SQL statement that SQL Service Broker depends on. For a complete list of limitations, see What are the limitations of SqlDependency.
The reason? The SQL statement that SQL Service Broker uses is converted, behind the scenes, into instructions to monitor the SQL Transaction Log for changes to the database. This monitoring is performed in the core of SQL Server, which makes it extremely fast when it comes to detecting changes to table(s). However, this speed comes at a cost: you can't use just any SQL statement, you must use one that can be converted into instructions to monitor the SQL Transaction Log.