SQLServer移动ALWASYON副本文件的方法和脚本

1.暂停ALWAYSON数据传送

ALTER DATABASE [db1] SET HADR SUSPEND;

ALTER DATABASE [db2] SET HADR SUSPEND;

ALTER DATABASE [db3] SET HADR SUSPEND;

2.生成脚本:



SELECT  database_id,

(sum(size)*8/1024/1024) as usedGb

FROM    sys.master_files 

WHERE   database_id in (

select database_id from sys.databases 

where

 NAME NOT IN ('....')

)  

AND physical_name LIKE 'E:\%' group  by database_id

order by  2 desc

3.生成脚本2

SELECT  name , 

        physical_name AS CurrentLocation ,

        state_desc,(size*8/1024/1024) as usedGb

,'ALTER DATABASE ['+DB_NAME(database_id)+'] MODIFY FILE ( NAME = '+name+' , FILENAME = '''+REPLACE(physical_name,'D:\','F:\')+''' )' 

FROM    sys.master_files 

WHERE   database_id in (17)  

AND  SIZE>1024*1024  AND physical_name LIKE 'E:\%' order by database_id

4.停SQLSERVER服务

4.1.剪切文件到新位置

4.2.启动SQLSERVER服务

5.恢复ALWAYSON数据传送

ALTER DATABASE [bdg_sub2] SET HADR RESUME;

>> Home

51ak

2020/02/02

Categories: sqlserver 故障处理 Tags: 整理

《数据库工作笔记》公众号
扫描上面的二维码,关注我的《数据库工作笔记》公众号