惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
Google DeepMind News
Google DeepMind News
G
Google Developers Blog
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
月光博客
月光博客
Jina AI
Jina AI
宝玉的分享
宝玉的分享
人人都是产品经理
人人都是产品经理
D
DataBreaches.Net
V
V2EX
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI
B
Blog
博客园 - 叶小钗
小众软件
小众软件
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
A
About on SuperTechFans
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog

博客园 - ok_008

解决GitHub加载和下载慢问题 mysql命令行爱好者必备工具mycli What is a Back Order mysql中实现字符串分割sp_split Down Payment 和 Deposit的差异 Select逻辑顺序图 排名趋势公式 批量备份数据库脚本(PowerShell版) 生成建表脚本up_CreateTable Data Model for Certificate Implementation of Message Receiver Data Model for Message Receiver SQL SERVER 批量生成编号 修改hosts文件 PHP-问题处理验证码无法显示出来 PHP-问题处理Fatal error: Uncaught Error: Call to undefined function mb_strlen() PHP-问题处理Fatal error: Uncaught Error: Call to undefined function simplexml_load_file() 10进制转33进制 PHP-生成缩略图和添加水印图-学习笔记 SQLServer地址搜索性能优化例子
批量替换存储过程内容脚本sp_SqlReplace
ok_008 · 2017-04-11 · via 博客园 - ok_008

开始

在数据库开发过程中,如果某一个表字段名被重命名。那么在使用到该字段的存储过程,对应的表字段名也要修改。

当存在多个存储都有使用该表字段,需要逐个去修改替换,是一件比较繁琐的事情,我们需要一个能实现批量替换的方法。

这里我写了一个批量替换存储过程内容的脚本:

sp_SqlReplace

Use master
Go
if object_ID('[sp_SqlReplace]') is not null
    Drop Procedure sp_SqlReplace
Go
create proc sp_SqlReplace
(
    @OriginalText nvarchar(max),
    @CurrentText nvarchar(max)
)
as  
Set Nocount On

Declare @Count int=0,@i int=1,@sql1 nvarchar(max),@sql2 nvarchar(max),@objectname sysname;
Declare @tblTmp Table(ID int identity primary key,objectID int,objectName sysname)

Insert Into @tblTmp(objectID,objectName) Select distinct  id,object_name(id) From sys.syscomments Where text like '%'+@OriginalText+'%'
Set @Count=@@ROWCOUNT

If @Count=0 Return 

Begin Try
    Begin Transaction
    While @i<=@Count
    Begin
        Select @sql1='if object_id('''+quotename(objectName)+''') Is Not null Drop '+case  when object_id(objectName,N'P') is not null then 'Procedure ' when Coalesce(object_id(objectName,N'FN'),object_id(objectName,N'IF'),object_id(objectName,N'TF')) is not null then 'Function ' when object_id(objectName,N'TR') is not null then 'Trigger ' else 'View 'end+Quotename(objectName) ,
                @sql2=Replace(object_definition(objectID),@OriginalText,@CurrentText),
                @objectname=objectName
            From @tblTmp 
            Where ID=@i
            
        Exec (@sql1)    
        Print @objectname
        
        Exec (@sql2)
        
        Set @i+=1
    End    
    Commit Transaction
    Select N'影响到对象有:' As [信息提示]
    Select 'Exec sp_sql '+objectName As [对象名称],case  when object_id(objectName,N'P') is not null then 'Procedure ' when Coalesce(object_id(objectName,N'FN'),object_id(objectName,N'IF'),object_id(objectName,N'TF')) is not null then 'Function ' when object_id(objectName,N'TR') is not null then 'Trigger ' else 'View 'end As [类型] From @tblTmp
End Try
Begin Catch    
    Rollback transaction
    throw
End Catch
Go

 调用方法