When authoring an INSERT Trigger for SQL Server 2008 Express in C#: I find that the below SqlTrigger annotation is required:
[SqlTrigger(Name = "SomeMethod", Target = TABLE_NAME, Event = "INSTEAD OF INSERT")]
public static void SomeMethod() { ... }
The Target specification seems like an annoying requirement. I would like to have this trigger available to be run on a variety of tables. Is there anyway to avoid specifying the Target or maybe to override it during creation time in the SQL?
Similar to this question:
SQL CLR Trigger – get Target / Table name
but I want to be able to dynamically set the Target.
My reason for exploring this, is I have some legacy code that is behaving badly. I want to be able to diagnose a problem potentially in the assembly trigger without shutting it down. Ideally, hijacking the inserts destined for it, doing some diagnostics, then passing back to the original assembly. I believe this Target parameter restricts me from doing this.
The
SqlTriggerAttributecontrols the registration of the trigger against a particular table. If you choose to, you can omit this attribute entirely, and perform registrations manually. You can reference the same CLR trigger code for multiple tables, as long as you do the registration through SQL:SomeMethodwill then be invoked when inserts are performed against eitherTable1orTable2.All of the above being said, however, it’s usually a bad sign if your database contains multiple tables with similar enough structure that it even makes sense for the same trigger code to run against all of them. Joe Celko used to refer to this issue as “Attribute Splitting” (specifically, Table Splitting).