Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Searching for a procedure that contains a keyword

Author  Topic 

SamC
White Water Yakist

3467 Posts

Posted - 2003-08-18 : 17:15:11
I've got about a hundred stored procedure files. It would be great to get search working so I could find all procedure files that had a keyword like "tablename" or whatever.

I've got two laptops, and neither one can find a sql file using the Start, Find Files or Folders. I always get no file found.

Is there another search tool, or can anyone guess what may be the problem?

Sam

nr
SQLTeam MVY

12543 Posts

Posted - 2003-08-18 : 17:20:17
Odd, I've just searched a server for this.

select OBJECT_NAME(ID)
from syscomments
where text like '%tablename%'

You can restrict it to SPs if you want.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-08-18 : 17:33:52
You are searching the file system for stored procedures? Are you using wild characters for the search? Instead of using the Windows search, just search within SQL Server like Nigel mentioned or by using this stored procedure:


CREATE PROCEDURE [dbo].[isp_search_code]

(
@SearchStr varchar(100),
@RowsReturned int = NULL OUT
)
AS
/*************************************************************************************************
Copyright © 1997 - 2002 Narayana Vyas Kondreddi. All rights reserved.

Purpose: To search the stored proceudre, UDF, trigger code for a given keyword.

Written by: Narayana Vyas Kondreddi
http://vyaskn.tripod.com

Added to GT database by Vladimir Harabor.

Tested on: SQL Server 7.0, SQL Server 2000

Date created: January-22-2002 21:37 GMT

Date modified: February-17-2002 19:31 GMT

Email: vyaskn@hotmail.com

Examples:

To search your database code for the keyword 'unauthorized':
EXEC isp_search_code 'unauthorized'

To search your database code for the keyword 'FlowerOrders' and also find out the number of hits:
DECLARE @Hits int
EXEC isp_search_code 'FlowerOrders', @Hits OUT
SELECT 'Found ' + LTRIM(STR(@Hits)) + ' object(s) containing this keyword' AS Result
*************************************************************************************************/
BEGIN
SET NOCOUNT ON

SELECT DISTINCT USER_NAME(o.uid) + '.' + OBJECT_NAME(c.id) AS 'Object name',
CASE
WHEN OBJECTPROPERTY(c.id, 'IsReplProc') = 1
THEN 'Replication stored procedure'
WHEN OBJECTPROPERTY(c.id, 'IsExtendedProc') = 1
THEN 'Extended stored procedure'
WHEN OBJECTPROPERTY(c.id, 'IsProcedure') = 1
THEN 'Stored Procedure'
WHEN OBJECTPROPERTY(c.id, 'IsTrigger') = 1
THEN 'Trigger'
WHEN OBJECTPROPERTY(c.id, 'IsTableFunction') = 1
THEN 'Table-valued function'
WHEN OBJECTPROPERTY(c.id, 'IsScalarFunction') = 1
THEN 'Scalar-valued function'
WHEN OBJECTPROPERTY(c.id, 'IsInlineFunction') = 1
THEN 'Inline function'
END AS 'Object type',
'EXEC sp_helptext ''' + USER_NAME(o.uid) + '.' + OBJECT_NAME(c.id) + '''' AS 'Run this command to see the object text'
FROM syscomments c
INNER JOIN
sysobjects o
ON c.id = o.id
WHERE c.text LIKE '%' + @SearchStr + '%' AND
encrypted = 0 AND
(
OBJECTPROPERTY(c.id, 'IsReplProc') = 1 OR
OBJECTPROPERTY(c.id, 'IsExtendedProc') = 1 OR
OBJECTPROPERTY(c.id, 'IsProcedure') = 1 OR
OBJECTPROPERTY(c.id, 'IsTrigger') = 1 OR
OBJECTPROPERTY(c.id, 'IsTableFunction') = 1 OR
OBJECTPROPERTY(c.id, 'IsScalarFunction') = 1 OR
OBJECTPROPERTY(c.id, 'IsInlineFunction') = 1
)

ORDER BY 'Object type', 'Object name'

SET @RowsReturned = @@ROWCOUNT
END



Tara
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2003-08-18 : 18:46:00
Tara and Nigel win the performance contest.

It's humbling to see how fast sql can be when you're used to dos/windows file search. I am getting old enough to feel a real old-timer statement coming on: "Whippersnapper! Why I remember the days when we did a search! We knew we were doing a search! We used to watch the disk light and made tea while listening to the disk head seek".... It was an IBM 1800 (hey, it was an antique even when I played with it.)

Thanks ...

Sam
Go to Top of Page
   

- Advertisement -