What's the name of the SQL SP that locates a text string?
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! :)
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
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.
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!
精彩评论