码迷,mamicode.com
首页 > 其他好文 > 详细

批量重命名

时间:2017-08-03 14:59:23      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:history   before   status   mit   span   ide   color   信息   dea   

go 
IF OBJECT_ID(NameHistory, U) IS NOT  NULL 
    DROP TABLE NameHistory

CREATE TABLE NameHistory (
  id INT IDENTITY
 ,oldname VARCHAR(200) NULL
  ,newname VARCHAR(200) NULL
  ,type  VARCHAR(50) NULL
) ON [PRIMARY]

declare m_cursor cursor scroll for
select  name,xtype FROM SysObjects Where XType IN (U,V,P) and name NOT IN
  (NameHistory,sp_proc_replace,TBaseInfo,TDealer,TDepartment,TRegionMatch,TRole,TRolePermission,TUser,TUserRole)
 
open m_cursor
declare   @oldname nvarchar(128),@xtype NVARCHAR(50)
 
fetch next from m_cursor into @oldname,@xtype
while @@FETCH_STATUS=0
begin
  declare   @newname nvarchar(128)
   set @newname=VW_+@oldname
    print @oldname
    INSERT NameHistory (oldname, newname, type)
  VALUES (@oldname, @newname, @xtype);
    EXEC sys.sp_rename @oldname,@newname
    fetch next from m_cursor into @oldname,@xtype
end
 
close m_cursor
deallocate m_cursor


go

--------------------------------------------------------------------

IF OBJECT_ID(dbo.sp_proc_replace, P) IS NOT  NULL 
    DROP PROC dbo.sp_proc_replace

go
CREATE PROCEDURE dbo.sp_proc_replace
      @pxtye VARCHAR(50)=null
AS 

IF OBJECT_ID(master..all_proc_before_replace, U) IS NOT  NULL 
    DROP TABLE master..all_proc_before_replace

    SELECT  o.name AS proc_name,
           
            definition ,
            o.type ,
              AS remark
    INTO    master..all_proc_before_replace   --- 做备份,备份到master..all_proc_before_replace 
    FROM    sys.sql_modules s ,
            sys.objects o
           
    WHERE   o.object_id = s.object_id          
            AND o.type = @pxtye -- 更新类型
            AND s.definition IS NOT NULL --未加密的过程
     AND  o.name!=sp_proc_replace

--创建表to_replace_create_proc_error 收集执行替换后的过程报错信息 
IF OBJECT_ID(master..to_replace_create_proc_error, U) IS NOT  NULL 
    DROP TABLE master..to_replace_create_proc_error
CREATE TABLE master..to_replace_create_proc_error
        ( proc_name NVARCHAR(128) ,
          definition NVARCHAR(MAX) ,
          errormsg NVARCHAR(MAX) )




        DECLARE @proc_name NVARCHAR(128) ,
            @definition NVARCHAR(MAX) ,
          @type NVARCHAR(50)
       

        DECLARE c CURSOR FAST_FORWARD
        FOR
            SELECT  proc_name ,
                    definition,
              type
          
            FROM    master..all_proc_before_replace 
        
        OPEN c 
        FETCH NEXT FROM C INTO @proc_name, @definition,@type
        WHILE @@FETCH_STATUS = 0 
            BEGIN 
        
                IF 1=1--@definition LIKE ‘%‘+@old_str+‘%‘ --符合条件的过程,进行替换
                    BEGIN 

  declare name_cursor cursor scroll for
select  oldname,newname FROM NameHistory
 
open name_cursor
declare    @bname nvarchar(128),@aname nvarchar(128)--,@type NVARCHAR(50)
 
fetch next from name_cursor into @bname,@aname
while @@FETCH_STATUS=0
begin
 
                    
 SET @definition = REPLACE(@definition, @bname+CHAR(13), @aname++CHAR(13))
   SET @definition = REPLACE(@definition, @bname+CHAR(10), @aname++CHAR(10))
    --   SET @definition = REPLACE(@definition, @bname+CHAR(13)+CHAR(10), @aname++CHAR(13)+CHAR(10))
                        SET @definition = REPLACE(@definition, @bname+), @aname+))
                        SET @definition = REPLACE(@definition, @bname+,, @aname+,)        
                        SET @definition = REPLACE(@definition, @bname+ , @aname+ )                
                        SET @definition = REPLACE(@definition, @bname+., @aname+.)
                        SET @definition = REPLACE(@definition, [+@bname+], [+@aname+])


    fetch next from name_cursor into @bname,@aname
end
 
close name_cursor
deallocate name_cursor









                        BEGIN TRY 
                            BEGIN TRANSACTION drop_create_proc  

                if(@type=P)
                  BEGIN
                  EXEC (  DROP PROC [ + @proc_name +] ) -- 先DROP



                  
                    END
                  if(@type=V)
                     BEGIN
                EXEC (  DROP VIEW [ + @proc_name +] ) 
                   END
                
                
                                EXEC ( @definition )-- 再CREATE
                  PRINT @proc_name+-----------------------------
                PRINT @definition
                       PRINT -----------------------------
                                UPDATE  master..all_proc_before_replace -- 将已经替换后执行成功的,进行标记   
                                SET     remark = Y
                                WHERE   proc_name = @proc_name 
                            COMMIT TRANSACTION drop_create_proc
        
                        END TRY 
                        BEGIN CATCH 
                            ROLLBACK TRANSACTION drop_create_proc
                            INSERT  INTO master..to_replace_create_proc_error   --将已经替换后执行不成功的,收集报错信息内容
                                    SELECT  @proc_name ,
                                            @definition ,
                                            ERROR_MESSAGE()
                        END CATCH 
                    END 
                FETCH NEXT FROM C INTO @proc_name, @definition ,@type
       
            END 
        
        CLOSE c
        DEALLOCATE c 



        IF EXISTS ( SELECT  * FROM    master..to_replace_create_proc_error ) 
            BEGIN 
          SELECT  * FROM    master..to_replace_create_proc_error 
                ---- 人为返回报错,便于查找
              RAISERROR (50005, -- Message id.
           10,    -- Severity,
           1,     -- State,
           Nabcde);
              
            END 

GO
IF @pxtye IS NULL 
  BEGIN  
set    @pxtye=V
END


EXEC sp_proc_replace @pxtye=V
go
--EXEC sp_proc_replace @pxtye=‘P‘

 

批量重命名

标签:history   before   status   mit   span   ide   color   信息   dea   

原文地址:http://www.cnblogs.com/zhutiehan/p/7279441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!