SQLServer 本地移动发布/订阅/分发数据库文件并更改逻辑名称和物理文件名

时间:2022-09-12 19:03:49

--------------------------------------------------------------------------------------------------
--  发布库TestDB(停止日志读取器代理、队列读取器代理,禁止用户访问)
--------------------------------------------------------------------------------------------------
USE master
GO
SELECT file_id,name,type_desc,physical_name,state_desc 
FROM sys.master_files WHERE database_id = DB_ID('TestDB'); 
GO
ALTER DATABASE TestDB SET SINGLE_USER --WITH ROLLBACK IMMEDIATE  
GO
ALTER DATABASE TestDB MODIFY FILE ( NAME = TestDB, NEWNAME= 'NewFileName', FILENAME = "E:\DatabaseFile\TestDB\NewFileName.MDF")
ALTER DATABASE TestDB MODIFY FILE ( NAME = TestDB_log, NEWNAME= 'NewFileName_log', FILENAME = "E:\DatabaseFile\TestDB\NewFileName_log.LDF")
GO
ALTER DATABASE TestDB SET OFFLINE
GO
EXEC sp_configure 'show advanced options',1
RECONFIGURE
GO
EXEC sp_configure 'xp_cmdshell',1
RECONFIGURE
GO
EXEC master..xp_cmdshell 'MOVE "E:\DatabaseFile\TestDB\TestDB.MDF" E:\DatabaseFile\TestDB\'
EXEC master..xp_cmdshell 'MOVE "E:\DatabaseFile\TestDB\TestDB_log.LDF" E:\DatabaseFile\TestDB\'
GO
EXEC master..xp_cmdshell 'ren "E:\DatabaseFile\TestDB\TestDB.MDF" "NewFileName.mdf"'
EXEC master..xp_cmdshell 'ren "E:\DatabaseFile\TestDB\TestDB_log.LDF" "NewFileName_log.ldf"'
GO
EXEC sp_configure 'xp_cmdshell',0
RECONFIGURE
GO
EXEC sp_configure 'show advanced options',0
RECONFIGURE
GO
ALTER DATABASE TestDB SET ONLINE
GO
ALTER DATABASE TestDB SET MULTI_USER
GO

--------------------------------------------------------------------------------------------------
--  订阅库TestDBSub (停止分发代理、队列读取器代理,禁止用户访问)
--------------------------------------------------------------------------------------------------
USE master
GO
SELECT file_id,name,type_desc,physical_name,state_desc 
FROM sys.master_files WHERE database_id = DB_ID('TestDBSub'); 
GO
ALTER DATABASE TestDBSub SET SINGLE_USER --WITH ROLLBACK IMMEDIATE  
GO
ALTER DATABASE TestDBSub MODIFY FILE ( NAME = TestDBSub, NEWNAME= 'NewFileName', FILENAME = "E:\DatabaseFile\TestDBSub\NewFileName.mdf")
ALTER DATABASE TestDBSub MODIFY FILE ( NAME = TestDBSub_log, NEWNAME= 'NewFileName_log', FILENAME = "E:\DatabaseFile\TestDBSub\NewFileName_log.ldf")
GO
ALTER DATABASE TestDBSub SET OFFLINE
GO
EXEC sp_configure 'show advanced options',1
RECONFIGURE
GO
EXEC sp_configure 'xp_cmdshell',1
RECONFIGURE
GO
EXEC master..xp_cmdshell 'MOVE "E:\DatabaseFile\TestDBSub\TestDBSub.mdf" E:\DatabaseFile\TestDBSub\'
EXEC master..xp_cmdshell 'MOVE "E:\DatabaseFile\TestDBSub\TestDBSub_log.ldf" E:\DatabaseFile\TestDBSub\'
GO
EXEC master..xp_cmdshell 'ren "E:\DatabaseFile\TestDBSub\TestDBSub.mdf" "NewFileName.mdf"'
EXEC master..xp_cmdshell 'ren "E:\DatabaseFile\TestDBSub\TestDBSub_log.ldf" "NewFileName_log.ldf"'
GO
EXEC sp_configure 'xp_cmdshell',0
RECONFIGURE
GO
EXEC sp_configure 'show advanced options',0
RECONFIGURE
GO
ALTER DATABASE TestDBSub SET ONLINE   
GO
ALTER DATABASE TestDBSub SET MULTI_USER
GO

--------------------------------------------------------------------------------------------------
--  分发库distribution(停止分发代理、日志读取器代理、队列读取器代理,禁止用户访问)
--------------------------------------------------------------------------------------------------
USE master
GO
SELECT file_id,name,type_desc,physical_name,state_desc 
FROM sys.master_files WHERE database_id = DB_ID('distribution');   
GO
ALTER DATABASE distribution SET SINGLE_USER --WITH ROLLBACK IMMEDIATE  
GO
ALTER DATABASE distribution MODIFY FILE ( NAME = distribution, NEWNAME= 'NewFileName', FILENAME = "E:\DatabaseFile\NewFileName.MDF")
ALTER DATABASE distribution MODIFY FILE ( NAME = distribution_log, NEWNAME= 'NewFileName_log', FILENAME = "E:\DatabaseFile\NewFileName_log.LDF")
GO
ALTER DATABASE distribution SET OFFLINE
GO
EXEC sp_configure 'show advanced options',1
RECONFIGURE
GO
EXEC sp_configure 'xp_cmdshell',1
RECONFIGURE
GO
EXEC master..xp_cmdshell 'MOVE "E:\DatabaseFile\distribution.MDF" E:\DatabaseFile\'
EXEC master..xp_cmdshell 'MOVE "E:\DatabaseFile\distribution.LDF" E:\DatabaseFile\'
GO
EXEC master..xp_cmdshell 'ren "E:\DatabaseFile\distribution.MDF" "NewFileName.mdf"'
EXEC master..xp_cmdshell 'ren "E:\DatabaseFile\distribution.LDF" "NewFileName_log.ldf"'
GO
EXEC sp_configure 'xp_cmdshell',0
RECONFIGURE
GO
EXEC sp_configure 'show advanced options',0
RECONFIGURE
GO
ALTER DATABASE distribution SET ONLINE
GO
ALTER DATABASE distribution SET MULTI_USER
GO

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

几个数据库的更改脚本都一样!~