I need to
restore the backup of production DB into the test server. The production linked
server name should be replaced with the test linked server but it is difficult
to find the procedures with specific linked server name. Below is the helpful
script which I used.
--
set "Result to Text" mode by pressing Ctrl+TSET NOCOUNT ONDECLARE
@sqlToRun
VARCHAR(1000), @searchFor VARCHAR(100), @replaceWith VARCHAR(100)-- text to search forSET @searchFor = '[MY-SERVER]'-- text to replace
withSET @replaceWith = '[MY-SERVER2]'-- this will hold
stored procedures textDECLARE @temp TABLE (spText VARCHAR(MAX))DECLARE curHelp CURSOR FAST_FORWARDFOR-- 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 stringSELECT 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 curHelpFETCH next FROM curHelp INTO @sqlToRunWHILE @@FETCH_STATUS = 0BEGIN--insert stored
procedure text into a temporary tableINSERT INTO @tempEXEC (@sqlToRun)-- add GO after each
stored procedureINSERT INTO @tempVALUES ('GO')FETCH next FROM curHelp INTO @sqlToRunENDCLOSE
curHelpDEALLOCATE curHelp-- find and replace
search string in stored procedures --
also replace CREATE PROCEDURE with ALTER PROCEDUREUPDATE @tempSET 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 runGO
I have
tested this in my environment and found it working fine.
No comments:
Post a Comment