tags:

views:

55

answers:

3

I am very embarrassed but I cannot remember the name of the MS SQL Server SP that searches your DB for a specified text string and returns all the table names, functions, stored procedures etc that contain that string. And I can't look it up in the help, because in order to get help on it, you need to know its name! It was something like sp_findtext.

What is it called?

Come on, fastest fingers gets answer credit! :)

+2  A: 

I found a custom SP (FindTextInDatabase) that does exactly that:

http://geekswithblogs.net/ram/archive/2007/05/22/112672.aspx

Don't know if there's a ready-made procedure also. If there is one, I can't find it either.

Cloud
+1 good one, but it doesn't search table/column names...
Shaul
ah didn't see that!
Cloud
A: 

From http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=32319

CREATE PROCEDURE sp_FindText @text varchar(8000), @findtype varchar(1)='P' AS
SET NOCOUNT ON
IF @findtype='P' SELECT DISTINCT Len(SubString(text,1, PatIndex('%' + @text + '%', text)))-Len(Replace(SubString(text,1, PatIndex('%' + @text + '%', text)),char(13),''))+1 AS Line,
--PatIndex('%' + @text + '%', text) AS Position,
OBJECT_NAME(id) AS ProcName
FROM syscomments
WHERE text like '%' + @text + '%'
ORDER BY ProcName, Line

IF @findtype='C' EXEC('SELECT TABLE_NAME + ''.'' + COLUMN_NAME AS TableColumn FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE ''%' + @text + '%'' ORDER BY TableColumn')

IF @findtype='T' EXEC('SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE ''%' + @text + '%'' ORDER BY TABLE_NAME')
GO

It not only searches procedure and view definition text, it will also find tables, views, and column names:

EXEC sp_FindText 'myTable' --or-- EXEC sp_FindText 'myTable', 'P' --finds procedures/views containing 'myTable' in their definition/code
EXEC sp_FindText 'myTable', 'T' --finds tables/views containing 'myTable' in their name
EXEC sp_FindText 'myCol', 'C' --finds columns containing 'myCol' in their name
Dave Barker
This is the best one, most versatile. Thanks!
Shaul
http://www.lmgtfy.com/?q=sp_findtext
Dave Barker
But wrong... syscomments only stores nvarchar(4000) so this fails if the search string of 4 letters starts at 3999 so this is unreliable
gbn
A: 

Have a quick check on the results of these:

select * from sysobjects where [name] like 'sp_%' order by 1
select * from master..sysobjects where [name] like 'sp_%' order by 1

You find what is it for you.

Cheers!

jerjer
You know that "ORDER BY 1" is completely useless, don't you?
Maximilian Mayerl
How is it useless? It orders by the first output column.
Tadmas
It would would be completely useless if you don't know what it is.
jerjer