-- 使用要被修改的DB(以下以old_db代稱,修改後的DB name用new_db)
USE old_db
-- 驗證DB真實名稱
SELECT file_id, name as logical_name, physical_name FROM sys.database_files
-- 使用Master DB
USE master
-- 修改 logical Name
ALTER DATABASE old_db MODIFY FILE (NAME=N'old_db', NEWNAME=N'new_db')
ALTER DATABASE old_db MODIFY FILE (NAME=N'old_db_log', NEWNAME=N'new_db_log')
設定DB只能由單一使用者存取
ALTER DATABASE old_db SET SINGLE_USER WITH ROLLBACK IMMEDIATE
-- 卸離資料庫
sp_detach_db 'old_db'
-- 改檔案名稱
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'xp_cmdshell', 1;
RECONFIGURE;
EXEC master..xp_cmdshell 'Rename D:\Database_Files\DB\old_db.mdf new_db.mdf'
EXEC master..xp_cmdshell 'Rename D:\Database_Files\Log\old_db_log.ldf new_db_Log.ldf'
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'xp_cmdshell', 0;
RECONFIGURE;
-- 接著到DB存放的位置直接修改File的檔案名稱
CREATE DATABASE old_db ON
(NAME = N'new_db', FILENAME = N'D:\Database_Files\DB\old_db.mdf new_db.mdf'),
(NAME = N'new_db_Log' , FILENAME = N'D:\Database_Files\Log\old_db_log.ldf new_db_Log.ldf')
FOR ATTACH
GO
ALTER DATABASE old_db SET MULTI_USER
ALTER DATABASE old_db MODIFY NAME = new_db;
MSSQL相關文章:
留言列表