设为首页 收藏本站
查看: 623|回复: 0

[经验分享] [SQL Server][FILESTREAM] -- How to Detach and Attach a SQL Server FILESTREAM En

[复制链接]

尚未签到

发表于 2018-10-13 11:55:59 | 显示全部楼层 |阅读模式
  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
DSC0000.png

  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
DSC0001.png

  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
DSC0002.png

  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>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
DSC0003.png

  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
DSC0004.png

  You can see in the above snippet that all the database files are now available in C:\FileStreamDB location.



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-621114-1-1.html 上篇帖子: SQL SERVER -- 错误:已超过了锁请求超时时段。 (Microsoft SQL Server,错误: 1222) 下篇帖子: LINQ to SQL异步查询
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表