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 2005 Forums
 Transact-SQL (2005)
 Search all Stored Procedures for character string

Author  Topic 

qman
Constraint Violating Yak Guru

442 Posts

Posted - 2010-02-18 : 10:35:20
Is it possible to query all Stored Procedures for a specific character string?

I need to replace the name of a Linked Server hard coded in multiple Stored Procedures with another Linked Server.

There are too many Stored Procedures to manually look through.

Any thoughts?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-18 : 10:37:21
yup. just do like

select definition from sys.sql_modules where definition like '%' + @searchstring + '%'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

qman
Constraint Violating Yak Guru

442 Posts

Posted - 2010-02-18 : 10:43:42
Once again Visakh16, thanks! You guys rock!

On a side note, can this logic be applied to search throught all Views?
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-02-18 : 10:44:48
NB -- VISAKH has a better way of doing this!!!!

I posted a script a while ago to recompile all functions and sprocs. It builds a table containing all the sql text for each object as it does.


I've edited it to just get all the text instead and added views to the list. Hope it helps
/*** Extract SQL text for all user functions and procs ************************
**
** Charlie (2010-Feb-18)
**
******************************************************************************/

SET NOCOUNT ON

DECLARE @object VARCHAR(MAX)
DECLARE @type VARCHAR(2)
DECLARE @text VARCHAR(MAX)
DECLARE @error INT

IF OBJECT_ID('tempdb..#sqlText') IS NOT NULL DROP TABLE #sqlText
IF OBJECT_ID('tempdb..#objects') IS NOT NULL DROP TABLE #objects

CREATE TABLE #sqlText (
[Id] INT IDENTITY(1,1)
, [line] NVARCHAR(255)
)

CREATE TABLE #objects (
[name] VARCHAR(255)
, [type] VARCHAR(255)
, [text] VARCHAR(MAX)
)

DECLARE recCursor CURSOR LOCAL READ_ONLY FOR
SELECT
[name]
, [type]
FROM
sys.Objects
WHERE
[Type] IN ('P', 'FN', 'V')
AND [is_MS_shipped] = 0

OPEN recCursor

FETCH NEXT FROM recCursor INTO @object, @type

WHILE ( @@FETCH_STATUS = 0 ) BEGIN

-- display the object
RAISERROR(@object, 0 ,1) WITH NOWAIT

SET @text = ''
TRUNCATE TABLE #sqlText

-- Get the TSQL for the current object
INSERT #sqlText EXEC sp_helpText @object

-- Concatenate all lines together into a dynamic SQL string
SELECT @text = @text + [Line] FROM #sqlText ORDER BY [Id] ASC

-- Tidy up multiple spaces
WHILE ( CHARINDEX(' ', @text) > 0 ) SET @text = REPLACE(@text, ' ', ' ')

-- Change the CREATE x TO ALTER x
SELECT @text = REPLACE(@text, 'CREATE PROCEDURE', 'ALTER PROCEDURE')
SELECT @text = REPLACE(@text, 'CREATE FUNCTION', 'ALTER FUNCTION')


-- Put the object into our list
INSERT #objects ([name], [type], [text])
SELECT @object, @type, @text

FETCH NEXT FROM recCursor INTO @object, @type

END

CLOSE recCursor
DEALLOCATE recCursor

-- do your seach here

SELECT * FROM #objects
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-18 : 10:48:09
quote:
Originally posted by qman

Once again Visakh16, thanks! You guys rock!

On a side note, can this logic be applied to search throught all Views?



yup why not


SELECT sm.definition
FROM sys.sql_modules AS sm
JOIN sys.objects AS o
ON sm.object_id = o.object_id
where sm.definition like '%' + @searchstring + '%'
and o.type='v'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

qman
Constraint Violating Yak Guru

442 Posts

Posted - 2010-02-18 : 10:51:16
Great, thanks guys!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-18 : 10:54:12
welcome again

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -