Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if a table is affected by any trigger

Is there a way to determine if a trigger on any given table affects one other specific table?

My actual problems is rows being deleted from a table, and I am confident it happens from a trigger on another table. I need to find this "other table" (there are several hundred tabels).

Thx in advance!

Regards /Snedker

like image 341
Morten Snedker Avatar asked Dec 09 '25 14:12

Morten Snedker


1 Answers

You can use

SELECT def,
        t.*
FROM sys.triggers t
CROSS APPLY (SELECT OBJECT_DEFINITION(object_id)) C(def)
WHERE def LIKE '%DELETE%' AND def LIKE '%your_table%'

If that doesn't find anything and you want to broaden the search you can use

SELECT *
FROM sys.sql_modules
WHERE definition LIKE '%DELETE%' AND definition LIKE '%your_table%' 

If that still doesn't work and you think that the query might be being sent by an application you can use extended events to get to the bottom of things as per my answer here.

like image 154
Martin Smith Avatar answered Dec 12 '25 03:12

Martin Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!