Tuesday, October 11, 2011

Stored Proc References

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 + '%'

ASP.Net 5 - Simple Html Page App

Motivation As part of a recent undertaking to learn Angular JS I started using the beta version of ASP.Net 5.   I figured why not introdu...