DBA Tips Archive for SQL Server

  


[an error occurred while processing this directive]

No Title

[an error occurred while processing this directive]

by Jeff Hunter, Sr. Database Administrator

Contents

Introduction

Moving a FILESTREAM-enabled database is very similar to moving a user database. The method used to move a FILESTREAM-enabled database includes detaching the database, moving the physical files and FILESTREAM data container, and then bringing the database back online with the CREATE DATABASE command and the FOR ATTACH and CONTAINS FILESTREAM options.

This guide shows the steps necessary to move a FILESTREAM-enabled database within the same instance using SQL Server 2008 R2 and Transact-SQL as part of a planned relocation or scheduled maintenance operation. For more information on how to move user databases and system databases in SQL Server, see the following articles:

Prerequisites

To work through the example demonstrated in this article, you will obviously need to have a FILESTREAM-enabled database. This guide will use the AdventureWorks2008R2 sample database which is part of the AdventureWorks 2008R2 sample databases installation.

Click here for step-by-step instructions on how to install the AdventureWorks 2008R2 sample databases. You can also find a guide on how to create a FILESTREAM-enabled database on Microsoft's Developer Network (MSDN).

Move a FILESTREAM-Enabled Database

The following example moves one the FILESTREAM-enabled AdventureWorks sample databases named AdventureWorks2008R2 to a different directory location in the same SQL Server instance using the Detach/Attach method. The AdventureWorks2008R2 database contains one data file, AdventureWorks2008R2_Data.mdf, one log file, AdventureWorks2008R2_Log.ldf, and one FILESTREAM Data Container Documents2008R2; all of which are currently located in the F:\MSSQL\MSSQL10_50.MSSQLSERVER\MSSQL\DATA directory path.


USE Master Go SELECT DB_NAME(database_id) AS "Database Name" , type_desc AS "File Type" , name AS "Logical File Name" , physical_name AS "Physical File" , state_desc AS "State" FROM sys.master_files WHERE database_id = DB_ID('AdventureWorks2008R2'); Go Database Name File Type Logical File Name Physical File State --------------------- ----------- -------------------------- ------------------------------------------------------------------------- ------ AdventureWorks2008R2 ROWS AdventureWorks2008R2_Data F:\MSSQL\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\AdventureWorks2008R2_Data.mdf ONLINE AdventureWorks2008R2 LOG AdventureWorks2008R2_Log F:\MSSQL\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\AdventureWorks2008R2_Log.ldf ONLINE AdventureWorks2008R2 FILESTREAM FileStreamDocuments2008R2 F:\MSSQL\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Documents2008R2 ONLINE

The name of the FILESTREAM filegroup is Document2008R2FileStreamGroup which will be needed when we bring the database back online later in this section.


USE AdventureWorks2008R2 Go SELECT name AS "File Group Name" , type_desc AS "File Group Type" FROM sys.filegroups; Go File Group Name File Group Type ------------------------------ --------------------------------- PRIMARY ROWS_FILEGROUP Document2008R2FileStreamGroup FILESTREAM_DATA_FILEGROUP

The data file and FILESTREAM directory will be relocated to G:\MSSQL\Data while the log file will be moved to L:\MSSQL\Log.

  1. Detach the database.


    USE master Go EXEC sp_detach_db @dbname = 'AdventureWorks2008R2' Go

  2. Next, move the data files, FILESTREAM directory, and the log files from the current location (F:\MSSQL\MSSQL10_50.MSSQLSERVER\MSSQL\DATA) to the new location (G:\MSSQL\Data for data files and FILESTREAM directory and L:\MSSQL\Log for log files).


    [VMWINDOWS1] C:\> move F:\MSSQL\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\AdventureWorks2008R2_Data.mdf G:\MSSQL\Data\ 1 file(s) moved. [VMWINDOWS1] C:\> move F:\MSSQL\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\AdventureWorks2008R2_Log.ldf L:\MSSQL\Log\ 1 file(s) moved. [VMWINDOWS1] C:\> xcopy F:\MSSQL\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Documents2008R2 G:\MSSQL\Data\Documents2008R2 /e /i /h /r /x F:\MSSQL\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Documents2008R2\filestream.hdr F:\MSSQL\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Documents2008R2\3c4fdc7a-4fb8-4b12-9f1c-41e9d4b32f47\88239bbb-d8c1-4488-98b9-cb03b4fa5b21\0000001d-000004ff-00d0 F:\MSSQL\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Documents2008R2\3c4fdc7a-4fb8-4b12-9f1c-41e9d4b32f47\88239bbb-d8c1-4488-98b9-cb03b4fa5b21\0000001d-0000051e-0008 F:\MSSQL\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Documents2008R2\3c4fdc7a-4fb8-4b12-9f1c-41e9d4b32f47\88239bbb-d8c1-4488-98b9-cb03b4fa5b21\0000001d-0000051e-000f F:\MSSQL\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Documents2008R2\3c4fdc7a-4fb8-4b12-9f1c-41e9d4b32f47\88239bbb-d8c1-4488-98b9-cb03b4fa5b21\0000001d-0000051e-0016 F:\MSSQL\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Documents2008R2\3c4fdc7a-4fb8-4b12-9f1c-41e9d4b32f47\88239bbb-d8c1-4488-98b9-cb03b4fa5b21\0000001d-0000051e-001d F:\MSSQL\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Documents2008R2\3c4fdc7a-4fb8-4b12-9f1c-41e9d4b32f47\88239bbb-d8c1-4488-98b9-cb03b4fa5b21\0000001d-0000051e-0024 F:\MSSQL\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Documents2008R2\3c4fdc7a-4fb8-4b12-9f1c-41e9d4b32f47\88239bbb-d8c1-4488-98b9-cb03b4fa5b21\0000001d-0000051e-002b F:\MSSQL\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Documents2008R2\3c4fdc7a-4fb8-4b12-9f1c-41e9d4b32f47\88239bbb-d8c1-4488-98b9-cb03b4fa5b21\0000001d-0000051e-0032 F:\MSSQL\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Documents2008R2\3c4fdc7a-4fb8-4b12-9f1c-41e9d4b32f47\88239bbb-d8c1-4488-98b9-cb03b4fa5b21\0000001d-0000051e-0039 10 File(s) copied [VMWINDOWS1] C:\> rd /s /q F:\MSSQL\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Documents2008R2

  3. Re-attach the database and point to the files in the new location using the CREATE DATABASE command with the FOR ATTACH and CONTAINS FILESTREAM option.


    USE master Go CREATE DATABASE AdventureWorks2008R2 ON PRIMARY (NAME = 'AdventureWorks2008R2_Data', FILENAME = 'G:\MSSQL\Data\AdventureWorks2008R2_Data.mdf'), FILEGROUP Document2008R2FileStreamGroup CONTAINS FILESTREAM (NAME = 'FileStreamDocuments2008R2', FILENAME = 'G:\MSSQL\Data\Documents2008R2') LOG ON (NAME = 'AdventureWorks2008R2_Log', FILENAME = 'L:\MSSQL\Log\AdventureWorks2008R2_Log.ldf') FOR ATTACH Go

  4. After bringing the database online, verify the change in file locations by querying the the sys.master_files system-wide view.


    SELECT DB_NAME(database_id) AS "Database Name" , type_desc AS "File Type" , name AS "Logical File Name" , physical_name AS "Physical File" , state_desc AS "State" FROM sys.master_files WHERE database_id = DB_ID('AdventureWorks2008R2'); Go Database Name File Type Logical File Name Physical File State --------------------- ----------- -------------------------- -------------------------------------------- ------ AdventureWorks2008R2 ROWS AdventureWorks2008R2_Data G:\MSSQL\Data\AdventureWorks2008R2_Data.mdf ONLINE AdventureWorks2008R2 LOG AdventureWorks2008R2_Log L:\MSSQL\Log\AdventureWorks2008R2_Log.ldf ONLINE AdventureWorks2008R2 FILESTREAM FileStreamDocuments2008R2 G:\MSSQL\Data\Documents2008R2 ONLINE

About the Author

Jeffrey Hunter is an Oracle Certified Professional, Java Development Certified Professional, Author, and an Oracle ACE. Jeff currently works as a Senior Database Administrator for The DBA Zone, Inc. located in Pittsburgh, Pennsylvania. His work includes advanced performance tuning, Java and PL/SQL programming, developing high availability solutions, capacity planning, database security, and physical / logical database design in a UNIX / Linux server environment. Jeff's other interests include mathematical encryption theory, tutoring advanced mathematics, programming language processors (compilers and interpreters) in Java and C, LDAP, writing web-based database administration tools, and of course Linux. He has been a Sr. Database Administrator and Software Engineer for over 20 years and maintains his own website site at: http://www.iDevelopment.info. Jeff graduated from Stanislaus State University in Turlock, California, with a Bachelor's degree in Computer Science and Mathematics.



Copyright (c) 1998-2017 Jeffrey M. Hunter. All rights reserved.

All articles, scripts and material located at the Internet address of http://www.idevelopment.info is the copyright of Jeffrey M. Hunter and is protected under copyright laws of the United States. This document may not be hosted on any other site without my express, prior, written permission. Application to host any of the material elsewhere can be made by contacting me at jhunter@idevelopment.info.

I have made every effort and taken great care in making sure that the material included on my web site is technically accurate, but I disclaim any and all responsibility for any loss, damage or destruction of data or any other property which may arise from relying on it. I will in no case be liable for any monetary damages arising from such loss, damage or destruction.

Last modified on
Saturday, 23-Mar-2013 18:50:13 EDT
Page Count: 9000