You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)], you can move the data, log, and full-text catalog files of a user database to a new location by specifying the new file location in the FILENAME clause of the [ALTER DATABASE](../../t-sql/statements/alter-database-transact-sql.md) statement. This method applies to moving database files within the same instance [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]. To move a database to another instance of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] or to another server, use [backup and restore](../../relational-databases/backup-restore/back-up-and-restore-of-sql-server-databases.md) or [detach and attach operations](../../relational-databases/databases/move-a-database-using-detach-and-attach-transact-sql.md).
31
+
32
+
> [!NOTE]
33
+
> This article covers moving user database files. For moving system database files, see [Move System Databases](../../relational-databases/databases/move-system-databases.md).
32
34
33
35
## Considerations
34
36
When you move a database onto another server instance, to provide a consistent experience to users and applications, you might have to re-create some or all the metadata for the database. For more information, see [Manage Metadata When Making a Database Available on Another Server Instance (SQL Server)](../../relational-databases/databases/manage-metadata-when-making-a-database-available-on-another-server.md).
35
37
36
-
Some features of the [!INCLUDE[ssDEnoversion](../../includes/ssdenoversion-md.md)] change the way that the [!INCLUDE[ssDE](../../includes/ssde-md.md)] stores information in the database files. These features are restricted to specific editions of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]. A database that contains these features cannot be moved to an edition of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] that does not support them. Use the sys.dm_db_persisted_sku_features dynamic management view to list all edition-specific features that are enabled in the current database.
38
+
Some features of the [!INCLUDE[ssDEnoversion](../../includes/ssdenoversion-md.md)] change the way that the [!INCLUDE[ssDE](../../includes/ssde-md.md)] stores information in the database files. These features are restricted to specific editions of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]. A database that contains these features cannot be moved to an edition of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] that does not support them. Use the `sys.dm_db_persisted_sku_features` dynamic management view to list all edition-specific features that are enabled in the current database.
37
39
38
-
The procedures in this topic require the logical name of the database files. To obtain the name, query the name column in the [sys.master_files](../../relational-databases/system-catalog-views/sys-master-files-transact-sql.md) catalog view.
40
+
The procedures in this article require the logical name of the database files. To obtain the name, query the name column in the [sys.master_files](../../relational-databases/system-catalog-views/sys-master-files-transact-sql.md) catalog view.
39
41
40
42
Starting with [!INCLUDE[ssKilimanjaro](../../includes/sskilimanjaro-md.md)], full-text catalogs are integrated into the database rather than being stored in the file system. The full-text catalogs now move automatically when you move a database.
43
+
44
+
> [!NOTE]
45
+
> Make sure the service account for the [SQL Server Database Services service](../../database-engine/configure-windows/configure-windows-service-accounts-and-permissions.md) has permissions to the new file location in the file system. For more information, see [Configure File System Permissions for Database Engine Access](../../database-engine/configure-windows/configure-file-system-permissions-for-database-engine-access.md).
41
46
42
47
## Planned Relocation Procedure
43
48
To move a data or log file as part of a planned relocation, follow these steps:
44
49
45
50
1. For each file to be moved, run the following statement.
2. Run the following statement to bring the database offline.
52
57
53
-
```
58
+
```sql
54
59
ALTER DATABASE database_name SET OFFLINE;
55
60
```
61
+
62
+
This action requires exclusive access to the database. If another connection is open to the database, the ALTER DATABASE statement will be blocked until all connections are closed. To override this behavior, use the [`WITH <termination>` clause](../../t-sql/statements/alter-database-transact-sql-set-options.md#with-termination-). For example, to automatically rollback and disconnect all other connections to the database, use:
63
+
64
+
```sql
65
+
ALTER DATABASE database_name SET OFFLINE WITH ROLLBACK IMMEDIATE;
66
+
```
56
67
57
68
3. Move the file or files to the new location.
58
69
59
70
4. Run the following statement.
60
71
61
-
```
72
+
```sql
62
73
ALTER DATABASE database_name SET ONLINE;
63
74
```
64
75
65
76
5. Verify the file change by running the following query.
66
77
67
-
```
78
+
```sql
68
79
SELECT name, physical_name AS CurrentLocation, state_desc
69
80
FROM sys.master_files
70
81
WHERE database_id = DB_ID(N'<database_name>');
@@ -75,7 +86,7 @@ ms.author: wiassaf
75
86
76
87
1. For each file to be moved, run the following statement.
77
88
78
-
```
89
+
```sql
79
90
ALTER DATABASE database_name MODIFY FILE ( NAME = logical_name , FILENAME = 'new_path\os_file_name' );
80
91
```
81
92
@@ -87,7 +98,7 @@ ms.author: wiassaf
87
98
88
99
5. Verify the file change by running the following query.
89
100
90
-
```
101
+
```sql
91
102
SELECT name, physical_name AS CurrentLocation, state_desc
92
103
FROM sys.master_files
93
104
WHERE database_id = DB_ID(N'<database_name>');
@@ -105,21 +116,21 @@ ms.author: wiassaf
105
116
106
117
- For the default (MSSQLSERVER) instance, run the following command.
107
118
108
-
```
119
+
```console
109
120
NET START MSSQLSERVER /f /T3608
110
121
```
111
122
112
123
- For a named instance, run the following command.
113
124
114
-
```
125
+
```console
115
126
NET START MSSQL$instancename /f /T3608
116
127
```
117
128
118
129
For more information, see [Start, Stop, Pause, Resume, Restart the Database Engine, SQL Server Agent, or SQL Server Browser Service](../../database-engine/configure-windows/start-stop-pause-resume-restart-sql-server-services.md).
119
130
120
131
3. For each file to be moved, use **sqlcmd** commands or [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)] to run the following statement.
121
132
122
-
```
133
+
```sql
123
134
ALTER DATABASE database_name MODIFY FILE( NAME = logical_name , FILENAME = 'new_path\os_file_name' );
124
135
```
125
136
@@ -135,7 +146,7 @@ ms.author: wiassaf
135
146
136
147
8. Verify the file change by running the following query.
137
148
138
-
```
149
+
```sql
139
150
SELECT name, physical_name AS CurrentLocation, state_desc
140
151
FROM sys.master_files
141
152
WHERE database_id = DB_ID(N'<database_name>');
@@ -144,7 +155,7 @@ ms.author: wiassaf
144
155
## Examples
145
156
The following example moves the [!INCLUDE[ssSampleDBobject](../../includes/sssampledbobject-md.md)] log file to a new location as part of a planned relocation.
146
157
147
-
```
158
+
```sql
148
159
USE master;
149
160
GO
150
161
-- Return the logical file name.
@@ -172,12 +183,13 @@ WHERE database_id = DB_ID(N'AdventureWorks2012')
[Start, Stop, Pause, Resume, Restart the Database Engine, SQL Server Agent, or SQL Server Browser Service](../../database-engine/configure-windows/start-stop-pause-resume-restart-sql-server-services.md)
- [Start, Stop, Pause, Resume, Restart the Database Engine, SQL Server Agent, or SQL Server Browser Service](../../database-engine/configure-windows/start-stop-pause-resume-restart-sql-server-services.md)
0 commit comments