How can I know which stored procedure or trigger is using a table on SQL Server 2008 R2?
Search all code using sys.sql_modules
SELECT OBJECT_NAME(object_id)
FROM sys.sql_modules sm
WHERE definition LIKE '%Mytable%'
Or use Red Gate SQL Search which is completely free
Do not use syscomments or INFORMATION_SCHEMA.RUBBISH
Try out ApexSQL Search
ApexSQL Search is a FREE SQL Server Management Studio and Visual Studio add-in that, among other features, has the View Dependencies feature. The View Dependencies feature has the ability to visualize all SQL database objects’ relationships, including those between encrypted and system objects, SQL server 2012 specific objects, and objects stored in databases encrypted with Transparent Data Encryption (TDE)
The View Dependencies feature also allows you to set up and customize the visual dependencies diagram layout, including the relationships that will be presented, the layout and size of the generated diagram, and the drill-down depth of the dependencies
Disclaimer: I work for ApexSQL as a Support Engineer
For future reference as of 2008 there is also a DMV that can be used. sys.dm_sql_referencing_entities
. I generally prefer it to using SQL_Modules as it avoids false positives among other things. I discussed it here but basically if you have a piece of code like this:
SELECT OBJECT_SCHEMA_NAME(object_id), OBJECT_NAME(object_id)
FROM sys.sql_modules WHERE [definition] LIKE '%ABC%'
You will end up with results for the table ABC, the table ABCLog the view vw_ABC, the stored procedure sp_Update_ABC etc. Also to the best of my knowledge the DMV will handle encrypted SPs as well as unencrypted while the sql_modules method only works with unencrypted SPs.
The DMV version of the same query is this:
SELECT * FROM sys.dm_sql_referencing_entities('dbo.ABC', 'OBJECT')