Solved

Find text / column in ALL stored procedures and replace, comments?

Posted on 2008-10-27
5
1,499 Views
Last Modified: 2012-06-21
Hi experts!

I had an issue where I needed to rename variable / text in all stored procedures where found. I looked everywhere (i think) for a solution and could not find one so I came up with attached code snippet.

Perhaps this could help others but for me I have allready noticed that if stored procedure is above 8000 chars (usually it is if structured propertly) it doesn't work. Is there other ways to accomplish what I am trying to do?

Thanks in advance.

PS. Did not use a temporary table (temp_procedure) as I wished to see what came out before EXEC(@sql) while debugging.



CREATE PROCEDURE dbo.sp_findandrename_all_storedproc

 @findtext nvarchar(500), 

 @replacetext nvarchar(500) 

AS

BEGIN
 

IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'temp_procedures') DROP TABLE temp_procedures  

CREATE TABLE temp_procedures(id int IDENTITY(1,1) ,sptext varchar(max))
 

  declare @procstr varchar(max)

  declare @astr varchar(max)

  declare @scid bigint

  declare @plid int

  declare @prid int

  

	--/ loop thru procedure 

       DECLARE @id CURSOR

       SET @id = CURSOR FOR 

        SELECT id FROM syscomments

          where text like '%'+@findtext+'%' --/ find procedures with defined text.

        OPEN @id

        FETCH NEXT FROM @id INTO @scid

         WHILE @@FETCH_STATUS = 0

          BEGIN

			SELECT @astr = so.name

			  FROM sysobjects so

			INNER JOIN syscomments sc ON so.id=sc.id

				   AND sc.id = @scid
 

              print('Extracted: '+@astr)

              

              --/ create temp table that stores procedure lines

              CREATE TABLE #proclines(id int IDENTITY(1,1) , procline varchar(max))

              INSERT INTO #proclines exec sp_helptext @astr

    	  

               SET @procstr = ''	
 

			   --/ loop thru lines and create a SQL string	

               DECLARE @id2 CURSOR

               SET @id2 = CURSOR FOR 

                SELECT id FROM #proclines

                OPEN @id2

                FETCH NEXT FROM @id2 INTO @plid

                 WHILE @@FETCH_STATUS = 0

                  BEGIN
 

                   select @procstr = @procstr + procline

                     from #proclines

                    where id = @plid
 

                   FETCH NEXT FROM @id2 INTO @plid

                  END

                CLOSE @id2

                DEALLOCATE @id2

                  	 

              --/ make changes in SQL string (procedure string)

			  SELECT @procstr = [dbo].[findandreplace] (@procstr,'CREATE PROC','ALTER PROC')

              SELECT @procstr = [dbo].[findandreplace] (@procstr,@findtext,@replacetext)

              

              --/ insert procedure string (SQL) into table so we can do EXEC record by record.

		      PRINT(@procstr)

			  INSERT INTO temp_procedures(sptext) values(@procstr)
 

              DROP TABLE #proclines

	

          FETCH NEXT FROM @id INTO @scid

          END

        CLOSE @id

        DEALLOCATE @id 

        

   DECLARE @sql varchar(max)
 

   --/ loop thru procedure table and execute SQL's

   DECLARE @id3 CURSOR

   SET @id3 = CURSOR FOR 

    SELECT id FROM temp_procedures

    OPEN @id3

    FETCH NEXT FROM @id3 INTO @prid

     WHILE @@FETCH_STATUS = 0

      BEGIN
 

       select @sql = sptext

         from temp_procedures

        where id = @prid
 

	   EXEC(@sql)

       PRINT(@sql)

       	

       FETCH NEXT FROM @id3 INTO @prid

      END

    CLOSE @id3

    DEALLOCATE @id3

    

   DROP TABLE temp_procedures         
 

END

Open in new window

0
Comment
Question by:westmediasystems
  • 2
  • 2
5 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22812516
  1. Script out all your stored procedures to a text file.
  2. do a search/replace using your favorite text editor.
  3. run the script to rebuild your procedures.
You can find all of them looking in syscomments as the script above does.  But you need to do a Create Procedure (or Alter Procedure) to change them -- hence the suggestion above.
0
 

Author Comment

by:westmediasystems
ID: 22814555
Actually the above snippet did both find procedures and alter them including execute, however... as mentioned the limit of 8000 chars was the issue. Some of the "alter procedure" sql statements became incomplete.

Anyhow, scripting into file is something I haven't done but I presume its easy to find, and of course, this sounds like a excellent way to accomplish what I am trying to do.

Thanks!
0
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 500 total points
ID: 22814601
>>Some of the "alter procedure" sql statements became incomplete.

Right ... those over 8000 characters are stored in multiple rows in syscomments (or whatever the 2005 table behind the syscomments view is).

I guess your script was more complete than I realized ... but scripting all the SP's to a file is pretty easy.

In SSMS, drill down under Programmability and click Stored Procedures.  In the right pane (Object Explorer Details) gang-select them, right-click and choose Script Stored Procedure As | Create To | and one of the 3 options.  I usually go to the clipboard & paste it into my text editor.
0
 
LVL 7

Expert Comment

by:Cedric_D
ID: 22815313
Firstly, for SQL2005 threshold is 4000 (not 8000) as it has nvarchar(4000) type.
Secondly, syscomments has PK with two columns: id + colid (procedure can have multiple chunks with the single id).

Create test procedure with more than 4000 chars, and see select * from syscomments.

So you should update your code, changing where you use single id, to use both id and colid.

There can be simplier version:

update syscomments set text = REPLACE(text, 'string1', 'string2')

(AdHoc updates should be allowed.  Make backup first!!!!).
But this version is not COMPLETELY correct because it will not replace strings at the boundaries of 4000-chunks.
If I have a little more time I can write a correct script  :)



0
 

Author Comment

by:westmediasystems
ID: 22815594
Hm, either this went straight over my head or I am simplpy a little bit slow here...
What I did was to retreive each line by using "exec sp_helptext" on procedures that had the text that was supposed to be located. Then I bundle every line together with find replace routine into a single column of varchar(8000). This was my @sql param so I could do an exec(@sql) that altered the procedure.

Are you talking about modifying the syscomment directly? If so, hope you get the time to write an correct script... :) Very intresting stuff imo.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now