您的位置:首页 > 数据库

[SQL Server][FILESTREAM] -- How to Detach and Attach a SQL Server FILESTREAM Enabled Database

2016-10-10 14:11 896 查看
From: https://www.mssqltips.com/sqlservertip/1878/how-to-detach-and-attach-a-sql-server-filestream-enabled-database/

Problem
Most SQL Server DBAs have questions about how to detach and attach a FILESTREAM enabled databases. In this tip, we will take a look at the steps Database Administrators need to follow in order to detach and attach a FILESTREAM database once Data, Log and FILESTREAM container files have been moved from the default location to a new location. This tip includes a general explanation of the FILESTREAM technology introduced with SQL Server 2008. This is followed by examples and scripts to detach and attach FILESTREAM enabled database in your environment.
Solution
In SQL Server 2008 one can store BLOBs (e.g. Images, Video, Word, Excel, PDF, MP3, etc files) in the NT file system rather than in a database file. This can be achieved by using the new FILESTREAM feature which was introduced in SQL Server 2008. However, the big question in the mind of many DBA is how FILESTREAM enabled databases can be detached and attached once Data, Log and FILESTREAM container files are moved from the default location to a new location. Are there any differences from a typical database? In this tip, we will go through an example of how to detach and attach a FILESTREAM enabled database.
Creating a FILESTREAM Enabled Database
Let us start by creating a FILESTREAM enabled database named FileStreamDB by executing the T-SQL code below.
Use Master
GO
IF EXISTS (SELECT name FROM sys.databases WHERE name = N'FileStreamDB')
DROP DATABASE FileStreamDB
GO
USE master
GO
CREATE DATABASE [FileStreamDB] ON PRIMARY
( NAME = N'FileStreamDB', FILENAME = N'D:\FileStreamDB\FileStreamDB.mdf',
SIZE = 10MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10% )
LOG ON
( NAME = N'FileStreamDB_log', FILENAME = N'D:\FileStreamDB\FileStreamDB_log.ldf' ,
SIZE = 10MB , MAXSIZE = UNLIMITED , FILEGROWTH = 10%)
GO
ALTER DATABASE [FileStreamDB]
ADD FILEGROUP [FileStreamGroup] CONTAINS FILESTREAM
GO
ALTER DATABASE [FileStreamDB]
ADD FILE (NAME = N'FileStreamDB_FSData', FILENAME = N'D:\FileStreamDB\FileStreamData')
TO FILEGROUP FileStreamGroup
GO

Creating a table with FILESTREAM columns
Let us now create the FileStreamDataStorage table by executing the T-SQL code below. This table will be used to store FILESTREAM data:
Use FileStreamDB
GO
IF EXISTS (SELECT name FROM sys.all_objects WHERE name = N'FileStreamDataStorage')
DROP TABLE FileStreamDataStorage
GO
CREATE TABLE [FileStreamDataStorage]
(
[ID] [INT] IDENTITY(1,1) NOT NULL,
[FileStreamData] VARBINARY(MAX) FILESTREAM NULL,
[FileStreamDataGUID] UNIQUEIDENTIFIER ROWGUIDCOL NOT NULL UNIQUE DEFAULT NEWSEQUENTIALID(),
[DateTime] DATETIME DEFAULT GETDATE()
)
ON [PRIMARY]
FILESTREAM_ON FileStreamGroup
GO
To store a BLOB using FILESTREAM feature, you must have a column of datatype VARBINARY (MAX) along with the FILESTREAM attribute. In addition to this, the table must also have a UNIQUEIDENTIFIER column with the ROWGUIDCOL attribute.
Inserting FILESTREAM Data
Let us now add a row to FileStreamDataStorage table by execute the below mentioned T-SQL code.
Use FileStreamDB
GO
INSERT INTO [FileStreamDataStorage] (FileStreamData)
SELECT * FROM
OPENROWSET(BULK N'C:\SampleFiles\Image1.BMP' ,SINGLE_BLOB) AS Document
GO
INSERT INTO [FileStreamDataStorage] (FileStreamData)
SELECT * FROM
OPENROWSET(BULK N'C:\SampleFiles\Image2.BMP' ,SINGLE_BLOB) AS Document
GO
INSERT INTO [FileStreamDataStorage] (FileStreamData)
SELECT * FROM
OPENROWSET(BULK N'C:\SampleFiles\Image3.BMP' ,SINGLE_BLOB) AS Document
GO
INSERT INTO [FileStreamDataStorage] (FileStreamData)
SELECT * FROM
OPENROWSET(BULK N'C:\SampleFiles\Image4.BMP' ,SINGLE_BLOB) AS Document
GO
/*
Execute the below mentioned TSQL code to retrieve the data from
FileStreamDataStorage table.
*/
USE FileStreamDB
GO
SELECT ID
, CAST([FileStreamData] AS VARCHAR) as [FileStreamData]
, FileStreamDataGUID
, [DateTime]
FROM [FileStreamDataStorage]
GO

For more information about inserting, updating or deleting FILESTREAM data, check out - Creating a SQL Server 2008 FILESTREAM Enabled Database and Using INSERT, UPDATE and DELETE statements to manage FILESTREAM Data.
Backup FILESTREAM Enabled Database
A DBA can perform a full backup of a FileStreamDB database by executing the T-SQL Code below. In this tip, all of the backups are using the database backup compression feature which was introduced in SQL Server 2008.
/* Perform a Full Backup of FileStreamDB */
Use master
GO
BACKUP DATABASE FileStreamDB
TO DISK =N'C:\DBBackup\FileStreamDB.BAK'
WITH COMPRESSION, INIT
GO

Once the database backups have successfully completed, the next step will be to go ahead and restore the FileStreamDB database.
For more information about How to Backup and Restore a SQL Server FILESTREAM Enabled Database, check out - How to Backup and Restore a SQL Server FILESTREAM Enabled Database.
Identify all the FILESTREAM Database Files
You can get the list of all the files which are related to FileStreamDB by executing the below mentioned T-SQL code.
/*
Identify all the Files which are related to FileStreamDB
*/
SELECT
file_id AS FileID,
file_guid AS FileGUID,
type_desc AS FileType,
name AS Name,
physical_name AS PhysicalName,
state_desc AS State
FROM FileStreamDB.sys.database_files
GO

In the above snippet you could see that FileStreamDB, FileStreamDB_Log and FileStreamDB_FSData files are located in D:\FileStreamDB.
Detach FileStreamDB Enabled Database
Let us now go ahead and detach the FileStreamDB database by executing the below mentioned T-SQL code.
USE [master]
GO
ALTER DATABASE [FileStreamDB] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
USE [master]
GO
EXEC master.dbo.sp_detach_db @dbname = N'FileStreamDB'
GO
Once the above TSQL code has executed successfully the FileStreamDB will be detached from your SQL Server 2008 instance.
Attach FileStreamDB Enabled Database
Let us now go ahead and attach the FileStreamDB database by executing the below mentioned T-SQL code. Here, you can see in the below code that I have mentioned the data and log file still SQL Server was able to attach the FileStreamDB to the SQL Server instance without any issues. However, things are much different if you plan to detach the database and then move the FileStreamDB, FileStreamDB_Log and FileStreamDB_FSData files from the default D:\FileStreamDB to any other location. We will see how to attach the FILESTREAM enabled database in such a scenario later down in this tip.
USE [master]
GO
CREATE DATABASE [FileStreamDB] ON
( FILENAME = N'D:\FileStreamDB\FileStreamDB.mdf' ),
( FILENAME = N'D:\FileStreamDB\FileStreamDB_log.ldf' )
FOR ATTACH
GO
As FileStreamDB, FileStreamDB_Log and FileStreamDB_FSData files were still present in the D:\FileStreamDB default location, you will be able to attach the database successfully without any issues.

Next, let us go ahead and detach the FileStreamDB database once again and then move the FileStreamDB folder from the D: drive to the C: drive. Once the move has successfully completed, you will be able to see all the three files in C:\FileStreamDB location.
1. FileStreamDB.mdf
2. FileStreamDB_log.ldf
3. FileStreamData (Folder)
Now, if you go ahead and attach the FileStreamDB database using the same script as above, except changing the file locations rom D: to C:, you will end up seeing the below error.
USE [master]
GO
CREATE DATABASE [FileStreamDB] ON
( FILENAME = N'C:\FileStreamDB\FileStreamDB.mdf' ),
( FILENAME = N'C:\FileStreamDB\FileStreamDB_log.ldf' )
FOR ATTACH
GO
Msg 5120, Level 16, State 105, Line 1
Unable to open the physical file "D:\FileStreamDB\FileStreamData". Operating system error 2: "2(The system cannot find the file specified.)".
Msg 5105, Level 16, State 14, Line 1
A file activation error occurred. The physical file name 'D:\FileStreamDB\FileStreamData' may be incorrect. Diagnose and correct additional errors, and retry the operation.
Msg 1813, Level 16, State 2, Line 1
Could not open new database 'FileStreamDB'. CREATE DATABASE is aborted.
Note: you also need to grant the permision of sql server start up user to folder 'D:\FileStreamDB\FileStreamData'.

The reason you get this error is because the database thinks the FILESTREAM data is still in the same location. Keep reading for how to solve this error.
Attaching a FILESTREAM database when FILESTREAM container is moved from the default location
Since I have copied all the Data, Log and FILESTREAM container from the default D:\FileStreamDB location to C:\FileStreamDB location; I was unable to attach the FILESTREAM enabled database. To overcome from this scenario, you need to mention the location of the FILESTREAM container within the attach database script as shown below.
USE [master]
GO
CREATE DATABASE [FileStreamDB] ON
( FILENAME = N'C:\FileStreamDB\FileStreamDB.mdf' ),
( FILENAME = N'C:\FileStreamDB\FileStreamDB_log.ldf' ),
FILEGROUP [FileStreamGroup] CONTAINS FILESTREAM DEFAULT
( NAME = N'FileStreamDB_FSData', FILENAME = N'C:\FileStreamDB\FileStreamData' )
FOR ATTACH
GO
However, you won't be able attach the FILESTREAM enabled database if the FILESTREAM container location is changed when you try to attach the database using SQL Server Management Studio. In such a scenario you should be using the above T-SQL code to attach the FILESTREAM enabled database.
Once the above script has executed successfully you can verify the location of all the FILESTREAM database files by executing the below mentioned T-SQL code.
/*
Identify all the Files which are related to FileStreamDB
*/
SELECT
file_id AS FileID,
file_guid AS FileGUID,
type_desc AS FileType,
name AS Name,
physical_name AS PhysicalName,
state_desc AS State
FROM FileStreamDB.sys.database_files
GO

You can see in the above snippet that all the database files are now available in C:\FileStreamDB location.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  default general enabled