4.5 Moving database files
4.5.1 Moving database files
In some scenarios, you need to move the files of the database to another path. If you allow the database to be taken offline, you can move the database files and then attach the database by detaching the database first.
You can also move a file while the database is online through a T-SQL statement, which takes effect the next time that the database comes back online. You need to use 2 parameters in the statement, the name parameter indicates the logical name of the database file, and the filename parameter indicates the path and physical file name of the moved target file.
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/77/37/wKioL1ZlUC6yh7TgAAB0CfW5Qzg023.png "title=" Database properties. png "alt=" Wkiol1zluc6yh7tgaab0cfw5qzg023.png "/>
| ALTER DATABASE [SQLDB01] MODIFY FILE (NAME = n ' fg2012_data ', FILENAME = n ' E:\MSSQL\Data\FG2012_data.ndf ') |
When you run the preceding statement, you must ensure that the destination path already exists, or SQL Server will return the error "The specified path is not in a valid directory."
After successful execution of the above statement, the entry for the logical file name has been modified in the system directory. The new path will be used the next time the database is started.
4.5.2 Emptying data files
Before you can delete a data file, you must empty the data in the data file.
|
Use [AdventureWorks2014]
GO
DBCC shrinkfile (N ' adventureworks2014_2 ', emptyfile)
GO |
4.5.3 Deleting data files
For an empty data file, you can delete it.
|
ALTER DATABASE [AdventureWorks2014] REMOVE FILE [Adventureworks2014_2]
GO |
This article from "SQLServer2014 series" blog, declined reprint!
4.5 Moving database files