Q) How do I find all the stored procs that reference tables containing the column named CompanyID
A)
select
cols.table_name,
so.name
from
information_schema.columns cols ,
syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
where
cols.column_name = 'CompanyID'
and sc.TEXT LIKE '%' + cols.table_name + '%'
The first thing is to find all the tables with company id
select * from information_schema.columns where column_name = 'CompanyID'
The second thing is to find all the stored procs that reference a table
----Option 1
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE '%tablename%'
----Option 2
SELECT DISTINCT o.name, o.xtype
FROM syscomments c
INNER JOIN sysobjects o ON c.id=o.id
WHERE c.TEXT LIKE '%tablename%'
thanks to pinal dave
http://blog.sqlauthority.com/2006/12/10/sql-server-find-stored-procedure-related-to-table-in-database-search-in-all-stored-procedure/
The last thing is to combine the two queries which was the select at the top
select
cols.table_name,
so.name
from
information_schema.columns cols ,
syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
where
cols.column_name = 'CompanyID'
and sc.TEXT LIKE '%' + cols.table_name + '%'