将数据/日志文件移动到其他位置
SQL Server存储命令
-- 1) Tell SQL Server the new path (nothing is moved yet):
ALTER DATABASE [Customer]
MODIFY FILE (NAME = N'Customer_data',
FILENAME = N'G:\Data\Customer_data.mdf');
-- 2) Take the database offline:
ALTER DATABASE [Customer] SET OFFLINE WITH ROLLBACK IMMEDIATE;
-- 3) Physically copy/move the file to the new path AT THE OPERATING SYSTEM level (G:\Data\...).
-- 4) Bring it online:
ALTER DATABASE [Customer] SET ONLINE;作用
更改数据库文件的物理位置:在元数据中定义新路径,在数据库脱机时物理移动文件,然后使数据库联机。
适用场景
更换磁盘或迁移到其他驱动器以获得空间时。
注意
OFFLINE + ROLLBACK IMMEDIATE 会断开所有活动连接;请规划停机时间。在操作系统中把文件实际复制到新路径之前,不要让数据库 ONLINE,否则数据库会变为 SUSPECT。tempdb 需要不同的方法(MODIFY FILE + 重启 SQL Server 服务)。