Getting Started with Adobe After Effects - Part 6: Motion Blur
A collection of quick technology learning tips from what people around you learn every day

Identify Trigger dependencies

Apr 16 2012 12:00AM by vanne040   

I stumbled across a scenario where I had to find all the triggers associated with few tables. Found the below queries and felt that they are worth sharing. I am listing the helpful sql queries!

To get a list of all the triggers in the database

select * from sysobjects where type = "TR"

To see all the objects related to a trigger

sp_depends <trigger_name>

To get the list of triggers based on a table name

select name 
from sysobjects 
where xtype='TR' 
and id in (select id from syscomments where text like '%MY-TABLE-NAME%')

To find the triggers for a list of tables(this is a good one)

SELECT 
    T.name AS TableName
    ,O.name  TriggerName  
    FROM sysobjects O 
    INNER JOIN sys.tables T ON T.object_id = O.parent_obj
    WHERE O.type = 'TR' AND T.name IN ('<table1>','<table2>',....)
ORDER BY TableName

During this time, I was doing some monkey testing and found out that its not possible to create a trigger under a schema different from the table schema (just a hope to know an additonal thing while I am there :-))

Important triggers tip : Triggers should always be written to handle multiple rows, using the inserted and deleted tables for joins instead of variables. Even if wealways just update single rows, coding this way will prevent issues if there is a multiple row change.More here

Read More..   [16 clicks]

Published under: SQL Server Tips ·  ·  ·  · 


vanne040
83 · 2% · 657
11
 
2
 
 
0
Incorrect
 
0
Interesting
 
0
Forgotten



Submit

6  Comments  

  • sp_depends has been deprecated. It's also fairly difficult to use in an automated way because it can return multiple result sets.

    Instead, use sys.dm_sql_referencing_entities and sys.dm_sql_referenced_entities

    commented on Apr 16 2012 6:17AM
    Marc Jellinek
    97 · 2% · 546
  • I agree with @Marc. DMVs should be used wherever possible.

    commented on Apr 16 2012 9:50PM
    Nakul Vachhrajani
    4 · 33% · 10585
  • Boy, do I agree with vanne040's last statement!!!

    commented on Apr 17 2012 2:42AM
    dishdy
    17 · 10% · 3262
  • @Dattatrey, while SQL Chick recommends querying [informationschema].[routines], I've done the same thing using [sys].[sqlmodules].

    I believe that [information_schema].[routines] is compliant with the ISO standard, while the [sys] schema is Microsoft proprietary.

    Any opnions on whether to use one or the other?

    commented on May 7 2012 9:22AM
    Marc Jellinek
    97 · 2% · 546
  • @Marc, the demonstration in SQL Chick's article is slightly different since she is not searching in Triggers. However, in general, if you look at the definition of INFORMATIONSCHEMA views, they in turn use the underlying system tables/views. Using sys schema should also be fine (specifically in this scenario since there is no INFORMATIONSCHEMA view which provides information related to Triggers as per my understanding).

    On the other hand INFORMATIONSCHEMA views provide a layer of abstraction and we don't need to worry about the changes in the underlying tables/views and INFORMATIONSCHEMA views change very rarely (the definition of the view might change to absorb the changes in the underlying tables/views but the output remains the same).

    commented on May 8 2012 1:38AM
    Dattatrey Sindol (Datta)
    43 · 4% · 1333

Your Comment


Sign Up or Login to post a comment.

"Identify Trigger dependencies" rated 5 out of 5 by 11 readers
Identify Trigger dependencies , 5.0 out of 5 based on 11 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]