Reputation: 32911
I am actually having 100's of SP in my database. I have to find a set of 10 SP's in that which have a particular comment inside them. Is there any search query for this.
Upvotes: 2
Views: 298
Reputation: 10987
I don't remember the exact tables names, but it would be something like this. Give it a try.
SELECT sysobjects.name
FROM syscomments, sysobjects
WHERE syscomments.id=sysobjects.id
WHERE syscomments.TEXT LIKE ‘%particular comment%’
Upvotes: 0
Reputation: 4311
Note that the syscomments search methods will fail if the String Search spans the boundary of records in syscomments.
To be 100% I guess you will have to Script the Sprocs out of the database
Suggest considering storing each Sproc in a separate file (and store in revision control repository etc.) rather than just Altering them in-situ in the DB - and then you can use your favourite editor to do a "Find" (and "Replace" if that is appropriate)
Upvotes: 2
Reputation: 4309
Found this in another SO question, credits go to the original answerer, gbn
SELECT
OBJECT_NAME(object_id)
FROM
sys.sql_modules
WHERE
definition LIKE '%' + 'WhatIWant' + '%'
It works for objects longer then 4k
Upvotes: 1
Reputation: 4188
Found this article which does exactly what your after if your using SQL Server
I'll certainly be keeping a copy of this code for my own use :)
Also, It doesn't just work for comments but it appears to work for all text in an SP.
Edit
I've included a copy of the code for simplicity but all credit goes to Les Smith
CREATE PROCEDURE Find_Text_In_SP
@StringToSearch varchar(100)
AS
SET @StringToSearch = '%' +@StringToSearch + '%'
SELECT Distinct SO.Name
FROM sysobjects SO (NOLOCK)
INNER JOIN syscomments SC (NOLOCK) on SO.Id = SC.ID
AND SO.Type = 'P'
AND SC.Text LIKE @stringtosearch
ORDER BY SO.Name
GO
Upvotes: 3
Reputation: 10732
I'm not sure if this is the best solution, but the following should work (although it may be slow):
Upvotes: 1
Reputation: 6955
you can use the information_schema for this:
select *
from information_schema.routines
where routine_definition like '%your comment to find%'
Bear in mind that this only looks through the first 4096 characters (i believe...) - to search through more characters, you have to query the underlying system tables directly. Have a look at the definition of the information_schema.routines view to see which system tables it is querying
Edit: @Rich's answer is what you will need if you need to query more than 4096 characters
Upvotes: 0
Reputation: 58491
select OBJECT_NAME(id), * from syscomments WHERE text LIKE 'WhateverYouAreSearchingFor'
Upvotes: 1