Skip to content

Commit e867617

Browse files
authored
Merge pull request #20597 from WilliamDAssafMSFT/20211102-file-sys-permissions
20211102 0828 update for issue 6987, refresh content
2 parents ae2c7a1 + 3b1b198 commit e867617

1 file changed

Lines changed: 37 additions & 25 deletions

File tree

docs/relational-databases/databases/move-user-databases.md

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
description: "Move User Databases"
3-
title: "Move User Databases | Microsoft Docs"
3+
title: "Move User Databases"
44
ms.custom: ""
5-
ms.date: "03/14/2017"
5+
ms.date: "11/02/2021"
66
ms.prod: sql
77
ms.prod_service: "database-engine"
88
ms.reviewer: ""
@@ -22,49 +22,60 @@ helpviewer_keywords:
2222
- "relocating database files"
2323
- "planned database relocations [SQL Server]"
2424
- "databases [SQL Server], moving"
25-
ms.assetid: ad9a4e92-13fb-457d-996a-66ffc2d55b79
2625
author: WilliamDAssafMSFT
2726
ms.author: wiassaf
2827
---
2928
# Move User Databases
3029
[!INCLUDE [SQL Server](../../includes/applies-to-version/sqlserver.md)]
3130
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).
3234
3335
## Considerations
3436
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).
3537

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.
3739

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.
3941

4042
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).
4146
4247
## Planned Relocation Procedure
4348
To move a data or log file as part of a planned relocation, follow these steps:
4449

4550
1. For each file to be moved, run the following statement.
4651

47-
```
52+
```sql
4853
ALTER DATABASE database_name MODIFY FILE ( NAME = logical_name, FILENAME = 'new_path\os_file_name' );
4954
```
5055

51-
2. Run the following statement.
56+
2. Run the following statement to bring the database offline.
5257

53-
```
58+
```sql
5459
ALTER DATABASE database_name SET OFFLINE;
5560
```
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+
```
5667

5768
3. Move the file or files to the new location.
5869

5970
4. Run the following statement.
6071

61-
```
72+
```sql
6273
ALTER DATABASE database_name SET ONLINE;
6374
```
6475

6576
5. Verify the file change by running the following query.
6677

67-
```
78+
```sql
6879
SELECT name, physical_name AS CurrentLocation, state_desc
6980
FROM sys.master_files
7081
WHERE database_id = DB_ID(N'<database_name>');
@@ -75,7 +86,7 @@ ms.author: wiassaf
7586

7687
1. For each file to be moved, run the following statement.
7788

78-
```
89+
```sql
7990
ALTER DATABASE database_name MODIFY FILE ( NAME = logical_name , FILENAME = 'new_path\os_file_name' );
8091
```
8192

@@ -87,7 +98,7 @@ ms.author: wiassaf
8798

8899
5. Verify the file change by running the following query.
89100

90-
```
101+
```sql
91102
SELECT name, physical_name AS CurrentLocation, state_desc
92103
FROM sys.master_files
93104
WHERE database_id = DB_ID(N'<database_name>');
@@ -105,21 +116,21 @@ ms.author: wiassaf
105116

106117
- For the default (MSSQLSERVER) instance, run the following command.
107118

108-
```
119+
```console
109120
NET START MSSQLSERVER /f /T3608
110121
```
111122

112123
- For a named instance, run the following command.
113124

114-
```
125+
```console
115126
NET START MSSQL$instancename /f /T3608
116127
```
117128

118129
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).
119130

120131
3. For each file to be moved, use **sqlcmd** commands or [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)] to run the following statement.
121132

122-
```
133+
```sql
123134
ALTER DATABASE database_name MODIFY FILE( NAME = logical_name , FILENAME = 'new_path\os_file_name' );
124135
```
125136

@@ -135,7 +146,7 @@ ms.author: wiassaf
135146

136147
8. Verify the file change by running the following query.
137148

138-
```
149+
```sql
139150
SELECT name, physical_name AS CurrentLocation, state_desc
140151
FROM sys.master_files
141152
WHERE database_id = DB_ID(N'<database_name>');
@@ -144,7 +155,7 @@ ms.author: wiassaf
144155
## Examples
145156
The following example moves the [!INCLUDE[ssSampleDBobject](../../includes/sssampledbobject-md.md)] log file to a new location as part of a planned relocation.
146157

147-
```
158+
```sql
148159
USE master;
149160
GO
150161
-- Return the logical file name.
@@ -172,12 +183,13 @@ WHERE database_id = DB_ID(N'AdventureWorks2012')
172183
```
173184

174185
## See Also
175-
[ALTER DATABASE &#40;Transact-SQL&#41;](../../t-sql/statements/alter-database-transact-sql.md)
176-
[CREATE DATABASE &#40;SQL Server Transact-SQL&#41;](../../t-sql/statements/create-database-transact-sql.md)
177-
[Database Detach and Attach &#40;SQL Server&#41;](../../relational-databases/databases/database-detach-and-attach-sql-server.md)
178-
[Move System Databases](../../relational-databases/databases/move-system-databases.md)
179-
[Move Database Files](../../relational-databases/databases/move-database-files.md)
180-
[BACKUP &#40;Transact-SQL&#41;](../../t-sql/statements/backup-transact-sql.md)
181-
[RESTORE &#40;Transact-SQL&#41;](../../t-sql/statements/restore-statements-transact-sql.md)
182-
[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)
186+
187+
- [ALTER DATABASE &#40;Transact-SQL&#41;](../../t-sql/statements/alter-database-transact-sql.md)
188+
- [CREATE DATABASE &#40;SQL Server Transact-SQL&#41;](../../t-sql/statements/create-database-transact-sql.md)
189+
- [Database Detach and Attach &#40;SQL Server&#41;](../../relational-databases/databases/database-detach-and-attach-sql-server.md)
190+
- [Move System Databases](../../relational-databases/databases/move-system-databases.md)
191+
- [Move Database Files](../../relational-databases/databases/move-database-files.md)
192+
- [BACKUP &#40;Transact-SQL&#41;](../../t-sql/statements/backup-transact-sql.md)
193+
- [RESTORE &#40;Transact-SQL&#41;](../../t-sql/statements/restore-statements-transact-sql.md)
194+
- [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)
183195

0 commit comments

Comments
 (0)