|
Finding and Manipulating Trigger Statuses
|
|
In SQL Server 2000, you can attach triggers to database objects like tables to
perform additional processing. For example, you can write a trigger to log
entries into a database. Triggers can exist in two states, enabled
and disabled. A trigger that is enabled will fire when the
appropriate DML (data manipulation language) statement is used (for example,
INSERT, UPDATE or DELETE). A trigger that is disabled will not fire when the
appropriate DML statement is used. Sometimes it is useful to know the status of
the triggers on a table or the status of all the triggers in the
database. Knowing this status can be useful to write many tools. For
example, if your database uses triggers and you want to provide a facility for
the application administrator to enable/disable the various triggers on a need
basis, you can easily write code that presents all the triggers in the database
along with their current status and the administrator can adjust the status
after which you execute the necessary commands against the database to
enable/disable the various triggers.
In this article, we will see how to determine the
status of the triggers for a table and for the entire database and also how you
can enable/disable individual triggers.
Before starting on the job, let's first create a table for
testing purposes. Create the following table in a database of your choice (I've
used the pubs database).
We do not need any data in this table, but we will create
two triggers on this table for the INSERT and UPDATE operation so
that we can play around with the trigger status. Here is the code the two
triggers:
Having now created the respective objects, let's now write
code to determine the status of the triggers on the above created
table. Instead of just writing the code, we will encapsulate the code inside a
user-defined function so that we can expand it later on in the code. Here is
the code for the function GetTriggerStatus.
The code above deserves some explanation.
Let's now see how to test this fuction. Here is the code for the same and the output:
In this output, we see that both the triggers are enabled. Let us see how to change the status of one of the triggers to disabled and again observe the output. Here is the code to disable a trigger.
The command to disable the trigger is by using the ALTER TABLE command. After this statement, if you again execute the function, you will see the following output:
OK, we have now seen how to get the trigger status for a single table. Let us now extend the function to be more useful. If no parameter is passed, the function should return the trigger status of all the triggers in the database. This dual behavior will allow us to provide the tool that we discussed earlier in this article by using the same function. Here is the code for the updated function.
The difference that you now see is that the WHERE clause is intelligent enough to detect if the table name passed is NULL. If so, it uses an equality condition that satisfies for all rows. Otherwise, it uses the old logic that we showed earlier. This allows to write the entire logic using only one statement. In fact, you can now easily convert this function into an inline table-valued function, which is much faster and works like a macro. Here is the updated code:
You can now execute this function using either a table name as a parameter or NULL as the parameter, in which case all the triggers and their statuses in the database are returned. Well, that brings us to the end of this article. In this article we have seen how to determine the status for a trigger and have written reusable code to work for both a single table and for the entire database. Along the way, we have also seen some useful commands that help give us the property of various SQL Server objects and also the name and object identifier of database objects. Finally, we have also seen two ways of writing user defined functions. You can write many useful tools using the code presented in this article and in a future article we shall see some of them. Have fun!! |
| Home |