码迷,mamicode.com
首页 > 数据库 > 详细

SqlServer:SqlServer(数据库备份,数据库迁移,增加数据库文件组,递归查询一周报送情况)

时间:2018-07-17 19:23:00      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:val   disk   备份   filename   limited   set   orm   repo   ldl   

1.数据备份

    
 DECLARE @databaseName varchar(600)
 DECLARE @str varchar(600) 
 DECLARE @savePath VARCHAR(600)
 DECLARE @date VARCHAR(60)=REPLACE(CONVERT(VARCHAR,GETDATE(),23),-,‘‘) 
 DECLARE @savename VARCHAR(600) 
 SET @savePath = f:/DatabaseBackup/
 DECLARE My_Cursor CURSOR 
    FOR 
    (
        select name from sys.databases 
        where name not like %tmp%
        and name not like %temp%
        and name not like %master%
        and name not like %model%
        and name not like %msdb%
        and name not like %wqb_upgrade%
        and name not like %ReportServer%
        and name not like %ReportServerTempDB%
        and name not like %spagobietllog%
        and name not like %spagobietl%
      
        
    )        
    OPEN My_Cursor;
    FETCH NEXT FROM My_Cursor INTO @databaseName
    WHILE @@FETCH_STATUS = 0
        BEGIN    
        
        --restore headeronly   from disk = ‘f:/doc/document_20180702.bak‘ 查看备份位置
            set @savename = @savePath+@databaseName+_+@date+.bak;
            declare @weekday varchar(50)
            declare @date2 varchar(100)
            declare @temppath varchar(100)
            select  @temppath= @savePath+@databaseName+_+REPLACE(CONVERT(VARCHAR, DATEADD(WK,DATEDIFF(wk,0,getdate()),0),23),-,‘‘)+.bak
            select @weekday = DATEname(weekday, getdate())
            
            if @weekday=星期一
                begin
                    set @temppath = @savename
                    INSERT INTO T_BACKUP_DATABASE_TIME(completeTime) values (getdate())                    
                    backup   database @databaseName to disk = @savename  with  FORMAT;
                end
            else
                begin
                    INSERT INTO T_BACKUP_DATABASE_TIME(diffTime) values (getdate())            
                    backup   database @databaseName to disk = @temppath  with differential;
                end
            FETCH NEXT FROM My_Cursor INTO @databaseName    
        END
    CLOSE My_Cursor; 
    DEALLOCATE My_Cursor;

 

2.数据迁移

 DECLARE @databaseName varchar(600)
 DECLARE @str varchar(6000) 
 DECLARE @tempstr varchar(6000)
 DECLARE @mName varchar(600)
 DECLARE @mlogName varchar(600)
 DECLARE @oldDataMovePath VARCHAR(600)
 DECLARE @oldLogMovePath VARCHAR(600)
 DECLARE @logMovePath VARCHAR(600)
 DECLARE @dataMovePath VARCHAR(600)
 DECLARE My_Cursor CURSOR 
    FOR 
    (
        
        select distinct name  from sys.databases 
         where name in
         (
         select 410000_yw_+UNIT_CODE from dbo.T_UNIT_AUDIT  
         where INDUSTRY_NAME ! = 重点国企
         )
        and name like %410000_yw_115211%
        and name not like %tmp%        
        and name not like %410000_yw_204101%
        and name not like %master%
        and name not like %model%
        and name not like %msdb%
        and name not like %NETWORKING_AUDIT%
        and name not like %ReportServer%
        and name not like %ReportServerTempDB%
        and name not like %wqb_upgrade%
        and name not like %temp%
        and name not like %spagobietllog%
        and name not like %Spagobietl%
        and name not like %李亚坤测试库%
    
    )        
    OPEN My_Cursor;
    FETCH NEXT FROM My_Cursor INTO @databaseName
    WHILE @@FETCH_STATUS = 0
        BEGIN    
            create table #tempt(name varchar(60));
            set @tempstr = 
                            DECLARE @mName varchar(600)
                            DECLARE @mlogName varchar(600)    
                            select @mName= name from [+@databaseName+]..sysfiles where name not like ‘‘%log%‘‘;
                            select @mlogName = name from [+@databaseName+]..sysfiles where name like ‘‘%log%‘‘;
                            insert into #tempt(name)values(‘‘‘‘+@mName+‘‘‘‘);
                            insert into #tempt(name)values(‘‘‘‘+@mlogName+‘‘‘‘);
            
            exec (@tempstr);
            select @mName= name from #tempt where name not like %log%;
            select @mlogName= name from #tempt where name  like %log%;
            set @oldDataMovePath = D:\DataBase\SqlServer\SqlServerDataBase\+@mName+.mdf
            set @oldLogMovePath = D:\DataBase\SqlServer\SqlServerLog\+@mlogName+.ldf
            set @dataMovePath  = F:\DataBase\SqlServer\SqlServerDataBase\+@mName+.mdf
            set @logMovePath = F:\DataBase\SqlServer\SqlServerLog\+@mlogName+.ldf
            set @str = alter database [+@databaseName+] set offline;
                    exec master.dbo.xp_cmdshell‘‘move +@oldDataMovePath+ F:\DataBase\SqlServer\SqlServerDataBase\‘‘;
                    exec master.dbo.xp_cmdshell‘‘move +@oldLogMovePath+ F:\DataBase\SqlServer\SqlServerLog\‘‘;        
                    alter database [+@databaseName+] modify file (name=‘‘‘+@mName+‘‘‘,filename=‘‘‘+@dataMovePath+‘‘‘);
                    alter database [+@databaseName+] modify file (name=‘‘‘+@mlogName+_1og‘‘,filename=‘‘‘+@logMovePath+‘‘‘);
                    alter database [+@databaseName+] set online;
            
            select (@str);
            
            FETCH NEXT FROM My_Cursor INTO @databaseName
            drop table #tempt;
        END
    CLOSE My_Cursor; 
    DEALLOCATE My_Cursor;

 

3.增加数据库文件组

        
 DECLARE @databaseName varchar(600)
 DECLARE @str varchar(6000) 
 DECLARE @dataPath VARCHAR(600)
 DECLARE @logPath VARCHAR(600)
 DECLARE My_Cursor CURSOR 
    FOR 
    (
        select name from sys.databases 
        where  name  like %tco%
        
    )        
    OPEN My_Cursor;
    FETCH NEXT FROM My_Cursor INTO @databaseName
    WHILE @@FETCH_STATUS = 0
        BEGIN    
            set @dataPath = F:\test\+@databaseName+1.ndf
            set @logPath = F:\test\+@databaseName+_log1.ldf        
            set @str =             
                DECLARE @dataSize varchar(50) 
                DECLARE @logSize varchar(50)
                DECLARE @innerstr varchar(6000) 
                select @dataSize = size*10  from  [+@databaseName+].[dbo].[sysfiles] where name not like ‘‘%log%‘‘;
                select @logSize = size*10  from  [+@databaseName+].[dbo].[sysfiles] where name like ‘‘%log%‘‘;
                set @innerstr = ‘‘ 
                    USE [master]
                    
                    ALTER DATABASE [+@databaseName+] MODIFY FILE ( NAME =N‘‘‘‘‘+@databaseName+‘‘‘‘‘,MAXSIZE = ‘‘+@dataSize+‘‘KB , FILEGROWTH = 1KB );
                    
                    ALTER DATABASE [+@databaseName+] MODIFY FILE ( NAME = N‘‘‘‘‘+@databaseName+_log‘‘‘‘,MAXSIZE = ‘‘+@logSize+‘‘KB , FILEGROWTH = 1KB );
                    
                    Alter database +@databaseName+ add file(NAME = ‘‘‘‘‘+@databaseName+1‘‘‘‘,FILENAME =‘‘‘‘‘+@dataPath+‘‘‘‘‘);
                    
                    Alter database +@databaseName+ add log file
                    (
                    name=‘‘‘‘‘+@databaseName+_log1‘‘‘‘,
                    filename=‘‘‘‘‘+@logPath+‘‘‘‘‘,
                    size=2MB,
                    maxsize=UNLIMITED,
                    filegrowth=10%
                    );    
                    ‘‘
                    exec(@innerstr)                
            
            exec (@str)
            
            FETCH NEXT FROM My_Cursor INTO @databaseName    
        END
    CLOSE My_Cursor; 
    DEALLOCATE My_Cursor;

 

4.查询一周报送情况

     WITH cte(t) AS
        (
            
            select DATEADD(DAY,-6,CAST(@date as date))AS t
            UNION ALL
            SELECT DATEADD(DAY,1,t)
            FROM cte
            WHERE t<CAST(@date as date)
        )
        SELECT case 
               when  t.RECEIVE_TIME IS null then 0
               else 1
               end as isReport,
        t1.RECEIVE_TIME,
        t1.UNIT_NAME,t1.UNIT_CODE
        FROM (
           select b.UNIT_CODE,b.UNIT_NAME,convert(varchar,a.t,23) RECEIVE_TIME
           from cte a
           cross join [NETWORKING_AUDIT].dbo.T_UNIT_AUDIT b
        ) as t1
        LEFT JOIN (
              select distinct a.UNIT_CODE,CONVERT(varchar,RECEIVE_TIME,23) RECEIVE_TIME 
                from [NETWORKING_AUDIT].dbo.T_UNIT_AUDIT  a 
                join [NETWORKING_AUDIT].dbo.T_FILE_RECEIVE_RECORD b 
                on a.UNIT_CODE = b.UNIT_CODE 
                and  convert(varchar,RECEIVE_TIME,23) > DATEADD(DAY,-7,CAST(@date as date))
                and  convert(varchar,RECEIVE_TIME,23) <= CAST(@date as date)
        ) t
        ON t1.RECEIVE_TIME=t.RECEIVE_TIME and t1.UNIT_CODE = t.UNIT_CODE
        order by t1.UNIT_CODE,t1.RECEIVE_TIME

 

SqlServer:SqlServer(数据库备份,数据库迁移,增加数据库文件组,递归查询一周报送情况)

标签:val   disk   备份   filename   limited   set   orm   repo   ldl   

原文地址:https://www.cnblogs.com/kuangzhisen/p/9324835.html

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