开发者

SQL Server Code Search Tool

I I wanted to search all stored procedures for the occurence of a substring "x", can I do this in SQL 2000? 2005? 2008?

..without any add-ins?

If n开发者_如何学编程ot, what are the best available free add ins for each?

Sorry. I should have mentioned that I am aware how to use sysobjects data to do this. But it seems to me there should be a nice GUI in front of this functionality!

FOR EXAMPLE, Something like this:

http://screencast.com/t/Y2Q0YmU0Nzkt


Also answered here

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


For SQL Server 2005 and 2008, use sys.sql_modules or OBJECT_DEFINITION which supply nvarchar(max) output

SELECT OBJECT_NAME(object_id) FROM sys.sql_modules WHERE definition LIKE @MyString
SELECT name FROM sys.objects WHERE OBJECT_DEFINITION(object_id) LIKE @MyString

For SQL Server 2000, you have to use sycomments or INFORMATION_SCHEMA.ROUTINES, but they supply nvarchar(4000) so are unreliable. You'd have to concat sys.comments into a text value and search that (and frankly I've forgotten how to do it off top of my head)

Other questions where exactly the same issue is mentioned

  • Find All References to View
  • Deleting a stored procedure in SQL server
  • SQL Server 2005 search views for certain database objects


if i were you i wouldn't use a tool - just run a script to do the search for you, i.e., the following SQL script will search all stored procs in a db for a string

Declare @search varchar(128)

SET @search = '%' + 'search text' + '%'


/***************************************************************************/

SELECT o.name As "Stored Procedures"

            FROM SYSOBJECTS o INNER JOIN SYSCOMMENTS c
                        ON o.id = c.id
            WHERE c.text LIKE @search
                        AND o.xtype = 'P'
            GROUP BY o.name
            ORDER BY o.name

/**************************************************************************/

In this instance the xtype 'P' refers to stored procedures

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜