Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
SQL

SQL Query to Find and Replace text in a stored procedures

- 20.09.19 | 22.11.19 - ErcanOPAK
Declare @spnames CURSOR
Declare @spname nvarchar(max)
Declare @moddef nvarchar(max)
Set @spnames = CURSOR FOR
select distinct object_name(c.id) 
from syscomments c, sysobjects o 
where c.text like '%findtext%'
and c.id = o.id
and o.type = 'P'    
OPEN @spnames
FETCH NEXT
FROM @spnames into @spname
WHILE @@FETCH_STATUS = 0
BEGIN   
    Set @moddef =
    (SELECT
    Replace ((REPLACE(definition,'findtext','replacetext')),'ALTER','create')
    FROM sys.sql_modules a
    JOIN 
    (   select type, name,object_id
    from sys.objects b
    where type in (
    'p' -- procedures
    )
    and is_ms_shipped = 0
    )b
    ON a.object_id=b.object_id where b.name = @spname)              
    exec('drop procedure dbo.' + @spname)
    execute sp_executesql @moddef
    FETCH NEXT FROM @spnames into @spname
END

Related posts:

SQL Deadlocks — The 3-Lock Pattern That Solves 90% Cases

Drop all the tables, stored procedures, triggers, constraints and all the dependencies in one sql st...

Capitalize words in SQL (Display data first letter uppercase rest lowercase)

Post Views: 291

Post navigation

Return number of rows affected by UPDATE statements in SQL
Create, Alter, Drop and Execute SQL Server Stored Procedures

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

December 2025
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
293031  
« Nov    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (915)
  • Get the First and Last Word from a String or Sentence in SQL (798)
  • How to select distinct rows in a datatable in C# (772)
  • How to add default value for Entity Framework migrations for DateTime and Bool (772)
  • How to make theater mode the default for Youtube (594)
  • Add Constraint to SQL Table to ensure email contains @ (558)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (534)
  • Average of all values in a column that are not zero in SQL (505)
  • Find numbers with more than two decimal places in SQL (421)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (416)

Recent Posts

  • Visual Studio High CPU When Idle? Live Code Analysis
  • WordPress Slow Admin Panel? Heartbeat API
  • Windows 11 Micro-Stutters in Apps? Disable MPO
  • Windows 11 Laptop Slow on Battery? CPU Throttling
  • AJAX Requests Fail Randomly? CSRF Token Missing
  • JS “await” Inside forEach Is a Bug
  • HTML5 Lazy Loading Without JavaScript
  • CSS 100vh Is Broken on Mobile (Real Fix)
  • ASP.NET Core Thread Pool Starvation Explained
  • ASP.NET Core Logging Slows Your API (Yes, Really)

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (915)
  • Get the First and Last Word from a String or Sentence in SQL (798)
  • How to select distinct rows in a datatable in C# (772)
  • How to add default value for Entity Framework migrations for DateTime and Bool (772)
  • How to make theater mode the default for Youtube (594)

Recent Posts

  • Visual Studio High CPU When Idle? Live Code Analysis
  • WordPress Slow Admin Panel? Heartbeat API
  • Windows 11 Micro-Stutters in Apps? Disable MPO
  • Windows 11 Laptop Slow on Battery? CPU Throttling
  • AJAX Requests Fail Randomly? CSRF Token Missing

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2025 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com