1) List stored procedures which contain a table.
Expand|Select|Wrap|Line Numbers
- SELECT DISTINCT syso.name FROM syscomments sysc INNER JOIN sysobjects syso ON sysc.id= syso.id WHERE sysc.TEXT LIKE '%tablename%' and upper(xtype)='P'
Here this query will return all the stored procedures using the given table.
Note: Keep your table name in place of: tablename in the above query.
What is syscomments object?.
The actual code for views, rules, defaults, triggers, CHECK constraints, DEFAULT constraints and stored procedures are stored in the syscomments table. The column TEXT in the syscomments table contains the actual code for all these objects. Knowing this allows you to write some simple T-SQL code that can scan the syscomments table looking for an actual table column name or database object name that we want to search.
2) List tables used in a stored procedure.
select name from sysobjects where id in
Expand|Select|Wrap|Line Numbers
- (select sd.depid from sysobjects so, sysdepends sd
- where so.name = 'usp_Your_Stored_Procedure' and sd.id = so.id ) and upper(xtype) = 'U'
Note: This will return only the tables used in this procedure.
If you want to get the views also then you have to use below query.
Expand|Select|Wrap|Line Numbers
- select name from sysobjects where id in
- (select sd.depid from sysobjects so, sysdepends sd
- where so.name = 'usp_getRepReport_by_OU' and sd.id = so.id ) and upper(xtype) in ('U','V')
Thanks
Bharath Reddy VasiReddy
Sr Software Engineer(RBC)