Quickest/Easiest way to use Search/Replace through all stored procedures

前端 未结 13 1075
萌比男神i
萌比男神i 2020-12-30 03:05

Actually, this is a 2 part question.

  1. Is it possible to use some sort of functionality to search through every stored procedure for a string and possibly rep

相关标签:
13条回答
  • 2020-12-30 03:20

    Update I just realized the link in David's answer included the search function. again, it's a great answer.


    David Atkinson's answer is great, just want to add the search part. (not sure when the search is added in SSMS, my version of SSMS V17.9.1)

    Instead of selecting stored procedure one by one, I can do search.

    • The search takes wildcard, similar to 'like' in TSQL

    0 讨论(0)
  • 2020-12-30 03:21

    From the Object Explorer Details window in SSMS, open the stored procedures folder. Select all the objects (you can multi-select from this window, which is pretty much the only purpose of the Object Explorer Details window) and right click, choosing to script as DROP and CREATE. You can now do a search/replace on this, replacing all you need in one go before executing it.

    Edit: I've blogged about this solution.

    0 讨论(0)
  • 2020-12-30 03:21

    I just run this code to find a specific text in all stored procedures:

    SELECT DISTINCT 
       o.name AS Object_Name,
       o.type_desc
     FROM sys.sql_modules m 
       INNER JOIN 
       sys.objects o 
         ON m.object_id = o.object_id
    WHERE m.definition Like '%textToFind%'
       or m.definition Like '%\[ifTextIsAColNameWithBrackets\]%' ESCAPE '\';
    
    0 讨论(0)
  • 2020-12-30 03:22

    You can search the text of the stored procedure definitions using this

    SELECT 
      Name 
    FROM 
      sys.procedures 
    WHERE 
       OBJECT_DEFINITION(OBJECT_ID) LIKE '%YourSearchText%'
    

    Replacing is generally a bad idea, since you don't know the context of the text you'll find in the stored procedures. It probably is possible though via Powershell scripting.

    I prefer this solution to any others, since I'm comfortable writing queries- so finding text in all stored procs, that are in schema (x) and database (y) and names that start with (z) is quite an easy and intuitive query.

    0 讨论(0)
  • 2020-12-30 03:27

    If you have downtime available.

    Go into "Generate scripts" and generate 'create' scripts for all of your sprocs you want to edit.

    Replace the text in the script and just drop and re-create all of them.

    0 讨论(0)
  • 2020-12-30 03:28

    I found this script where you can define search for and replace by text and simply run it to get text replaced in all procedures at once. I hope this will help you in bulk.

    -- set "Result to Text" mode by pressing Ctrl+T
    SET NOCOUNT ON
    
    DECLARE @sqlToRun VARCHAR(1000), @searchFor VARCHAR(100), @replaceWith VARCHAR(100)
    
    -- text to search for
    SET @searchFor = '[MY-SERVER]'
    -- text to replace with
    SET @replaceWith = '[MY-SERVER2]'
    
    -- this will hold stored procedures text
    DECLARE @temp TABLE (spText VARCHAR(MAX))
    
    DECLARE curHelp CURSOR FAST_FORWARD
    FOR
    -- get text of all stored procedures that contain search string
    -- I am using custom escape character here since i need to espape [ and ] in search string
    SELECT DISTINCT 'sp_helptext '''+OBJECT_SCHEMA_NAME(id)+'.'+OBJECT_NAME(id)+''' ' 
    FROM syscomments WHERE TEXT LIKE '%' + REPLACE(REPLACE(@searchFor,']','\]'),'[','\[') + '%' ESCAPE '\'
    ORDER BY 'sp_helptext '''+OBJECT_SCHEMA_NAME(id)+'.'+OBJECT_NAME(id)+''' '
    
    OPEN curHelp
    
    FETCH next FROM curHelp INTO @sqlToRun
    
    WHILE @@FETCH_STATUS = 0
    BEGIN
       --insert stored procedure text into a temporary table
       INSERT INTO @temp
       EXEC (@sqlToRun)
    
       -- add GO after each stored procedure
       INSERT INTO @temp
       VALUES ('GO')
    
       FETCH next FROM curHelp INTO @sqlToRun
    END
    
    CLOSE curHelp
    DEALLOCATE curHelp
    
    -- find and replace search string in stored procedures 
    -- also replace CREATE PROCEDURE with ALTER PROCEDURE
    UPDATE @temp
    SET spText = REPLACE(REPLACE(spText,'CREATE PROCEDURE', 'ALTER PROCEDURE'),@searchFor,@replaceWith)
    
    SELECT spText FROM @temp
    -- now copy and paste result into new window
    -- then make sure everything looks good and run
    GO
    

    Here is the reference link :
    http://www.ideosity.com/ourblog/post/ideosphere-blog/2013/06/14/how-to-find-and-replace-text-in-all-stored-procedures

    0 讨论(0)
提交回复
热议问题