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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 How to search for a string in sprocs

Author  Topic 

Chainwhip
Starting Member

33 Posts

Posted - 2003-07-22 : 08:06:43
I would like to know if it is possible to search for a string in all my stored procedures in all databases. For example I would like to have a list returned of all the stored procedures I have created that contain the variable @Test.

I can do this easily by "copying" all the sprocs from EM and then pasting into a text editor so I can search for a string. I would like to know if there is a quicker way than this, like a query that I can run in Query Analyzer that returns of database name and sproc name containing the string?

dsdeming

479 Posts

Posted - 2003-07-22 : 08:26:43
Try:

SELECT o.name
FROM sysobjects o
JOIN syscomments c ON o.id = c.id
WHERE c.text LIKE '%@Test%'

Dennis
Go to Top of Page

Andraax
Aged Yak Warrior

790 Posts

Posted - 2003-07-22 : 08:30:49
...or something like this for all DB's:

declare @dbname varchar(100)
declare @sql varchar(2000)

declare cu cursor for
select name from master..sysdatabases

open cu

fetch from cu into @dbname

while @@fetch_status=0
begin
select @sql='select distinct table_catalog, object_name(id) as object_name from ' + @dbname + '..syscomments, ' +@dbname + '.information_schema.tables where text like ''%text to find here%'''

exec (@sql)

fetch from cu into @dbname
end

close cu
deallocate cu

Go to Top of Page

Chainwhip
Starting Member

33 Posts

Posted - 2003-07-22 : 08:34:48
Thanks Dennis, that's 1/2 solved my task. I should be able to work the rest out from here to get a full list for all databases in one table...

Go to Top of Page

Chainwhip
Starting Member

33 Posts

Posted - 2003-07-22 : 08:40:36
Andraax, getting some error messages back here for each database...

Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'masterinformation_schema.tables'.

Go to Top of Page

Chainwhip
Starting Member

33 Posts

Posted - 2003-07-22 : 09:05:30
Decided to mix and match and got this to work (thanks fellas):-

declare @dbname varchar(100)
declare @sql varchar(2000)

CREATE TABLE #Query
(
strDB VARCHAR(50),
strSproc VARCHAR(100)
)

declare cu cursor for
select name from master..sysdatabases

open cu

fetch from cu into @dbname

while @@fetch_status=0
begin

SELECT @sql = 'INSERT #Query(strDB, strSproc) SELECT ''' + @dbname + ''' AS dbname, o.name FROM '+@dbname+'.dbo.sysobjects o JOIN '+@dbname+'.dbo.syscomments c ON o.id = c.id WHERE c.text LIKE ''%@Password%'''
EXEC(@sql)

fetch from cu into @dbname
end

close cu
deallocate cu

SELECT strDB, strSproc FROM #Query
DROP TABLE #Query

Go to Top of Page

Andraax
Aged Yak Warrior

790 Posts

Posted - 2003-07-22 : 09:54:48
Looks good!

As for the error, did you miss the dot before information_schema in the code?



Go to Top of Page

Chainwhip
Starting Member

33 Posts

Posted - 2003-07-22 : 10:34:42
No it's still giving me the same error:-

Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'master.information_schema.tables'.

I've not seen these information_schema.tables before - they're not in EM is this right?

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-07-22 : 10:55:52
How about just script all of the spocs and do a find on the text file.

I wouldn't inagine that the location of a "thing" is good with out the reference to it's location in a sproc.

What are you doing?



Brett

8-)
Go to Top of Page

Chainwhip
Starting Member

33 Posts

Posted - 2003-07-22 : 11:31:09
The script I have done works fine now.

It's difficult to work with a large text file as it's not always immediately apparanet which database or sproc I'm actually looking at.

I want to be easily able to track down for example which sprocs are calling insert commands to a global table, when changes to the global table require changes to the sprocs.

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-07-22 : 12:29:13
Ever look at sp_depends?



Brett

8-)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-07-22 : 12:38:20
Vyaskn wrote this one:


CREATE PROCEDURE [dbo].[sp_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

GO



Tara
Go to Top of Page

Andraax
Aged Yak Warrior

790 Posts

Posted - 2003-07-23 : 03:16:31
Oh Chain... You are not using SQL2000 right? That would explain why the information_schema views are not there.

Go to Top of Page

Chainwhip
Starting Member

33 Posts

Posted - 2003-07-23 : 04:47:26
Ah, yes I am using SQL 2000. That must explain things!

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-07-23 : 12:00:23
Chainwhip, the information schema views are available in SQL Server 2000 and you can see them in Enterprise Manager. Go to the master database in Enterprise Manager and then go to Views. You should see them there. They are owned by INFORMATION_SCHEMA. Let us know if you don't see them. The important thing is that you have to go to Views in EM and not tables.

Tara
Go to Top of Page

Andraax
Aged Yak Warrior

790 Posts

Posted - 2003-07-25 : 02:53:42
Ehhh... Misunderstanding there... :) I meant the views are there if you have SQL 2000, but not in earlier versions.

Go to Top of Page
   

- Advertisement -