tags:

views:

49

answers:

1

Is it possible to list all triggers in all tables on a database? I can list all user stored procedures with:

Private Sub StoredPro()
    Dim theServer As Server = New Server(serverName)
    Dim myDB As Database = theServer.Databases("mydatabase")
    Dim dt As DataTable = myDB.EnumObjects(DatabaseObjectTypes.StoredProcedure)
End Sub

But nothing obvious in SMO for triggers. (I'm actually trying to list just custom triggers, any that are not named DEL_tablename, INS_tablename, UPD_tablename) within the selected database.

A: 

Hey mate,
Each database object has a Triggers property, which you can iterate to find all triggers.
Once you have a reference to this trigger, check the IsSystemObject property to check if it is a user-defined trigger or not.

foreach ( DatabaseDdlTrigger oTrigger in oDatabase.Triggers) {
if (! oTrigger.IsSystemObject) {
    // do something
}}

Have fun.

blorkfish
Hi blorkfish, this doesn't seem to list any triggers, even without the if? (it hits the loop then exits so I guess it's not finding any triggers on the selected database.
madlan
Hey Madlan, The Microsoft.SqlServer.Management.Smo.Table object and the view object also has a Triggers property. If you know of a table that has a trigger, maybe try enumerating this property ?
blorkfish
I see, these are table triggers so could that explain?
madlan