Domanda

While using SqlDependency and SqlMonitor to get notified of database alterations I know there are certain rules to construct the query.

What I am curious about is what is the required structure of Stored Procedure's; as far as I can understand for making a simple SELECT query, by following the rules I can make it work with sql notification service.

  1. What happens when there are control flows (if's, else's or while's) in SP? Is there any rule set?

  2. Does same rules apply on Function's too?

È stato utile?

Soluzione

Creating a Query for Notification:

When a command that registers a notification contains more than one statement, the Database Engine creates a notification for each statement in the batch.

So if you have control flow (if, else, while etc) then the path that was actually executed is the path that sets up the notifications. If a branch is not taken then no notification is set up, as no statement is executed in that branch. If a loop executes a statement repeatedly, a separate notification is set up for every execution.

As for UDFs the only one that matter are those that are basically inlined in the statement, since pretty much any other UDF would make the query invalid for notifications. Those inlined UDFs act as if you had written the expanded function definition in the statement. In other words, they don't change anything vis-a-vis the notificaiton. Is the statement that matters.

The gist of it is that only SELECT can set up a notification. When you execute a SP (or a batch) the notification is not set up for the 'SP' per-se, but instead the notifications are set up for each SELECT statement executed, as is being executed.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top