December 21, 2006 at 10:35 pm
does anyone have a script to search the db for procs, functions that use certain tables?
December 22, 2006 at 12:34 am
select
distinct object_name(id) from syscomments where text like '%Table Name%'
MohammedU
Microsoft SQL Server MVP
December 22, 2006 at 9:48 am
ive used this in the past; it's a cursor, and resource intensive; it takes a couple of minutes to run on my 100meg database with 100's of procs and 1400+tables:
sample results:
ProcName | MightReferenceObject |
PR_ACTDELFAVORITES | GMACT |
PR_ACTDELFAVORITES | GMACTFAVORITES |
PR_COMPLETEDELETE_ACT | GMACTFAVORITES |
PR_COMPLETEDELETE_ACT | GMHOPWA6 |
PR_COMPLETEDELETE_ACT | GMHOPWA7 |
--search all procedures and find out if the name of any of the objects appear in their text:
SET NOCOUNT ON
declare
@isql varchar(2000),
@objname varchar(64)
--All User Tables, Views, Procedures, Table function or Scalar Function.
Create Table #MightReference(
ProcName varchar(60),
MightReferenceObject varchar(60)
)
declare c1 cursor for select name from sysobjects where xtype in ('U','V','P','TF','FN')
open c1
fetch next from c1 into @objname
While @@fetch_status <> -1
begin
--desired logic: if an object name exists in the syscomments of a procedure,
--it MIGHT mean the procedure references the object
--insert the two into a table for further evaulation.
Insert into #MightReference(ProcName,MightReferenceObject)
select object_name(id),@objname from syscomments
where id in(select id from sysobjects where xtype='P' and name NOT like 'dt_%')
and text like '%' + @objname + '%'
fetch next from c1 into @objname
end
close c1
deallocate c1
--avoid self referencing procs. "CREATE PROC MYPROC" statement returns itself!
Select * from #MightReference
where ProcName <> MightReferenceObject
order by ProcName
Select * from #MightReference
where ProcName <> MightReferenceObject
order by MightReferenceObject
Lowell
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply