[SQL Server][FILESTREAM] -- FileTable Feature in SQL Server 2012
2016-10-10 15:03
585 查看
From: https://www.mssqltips.com/sqlservertip/2667/filetable-feature-in-sql-server-2012/
ProblemThe FileTable feature of SQL Server 2012 is an enhancement to the FILESTREAM feature which was introduced in SQL Server 2008. In this tip we will take a look at how to use FileTable feature of SQL Server 2012.
SolutionA FileTable is a new user table which gets created within a FILESTREAM enabled database. Using the FileTable feature, organizations can now store files and documents within a special table in SQL Server and they will have the ability to access those files and documents from windows. When you use this feature it will appear to you as if the files and documents are residing on a file system rather than in SQL Server. However, in order to use the FileTable feature you need to enable the FILESTREAM feature on the instance of SQL Server 2012. Follow the steps mentioned in this tip, Different ways to enable FILESTREAM feature of SQL Server 2008, to enable the FILESTREAM feature on SQL Server 2012. Database administrators can define indexes, constraints and triggers; however the columns and system defined constrains cannot be altered or dropped. Also, in order to enable the FILESTREAM feature you need to be a member of the SYSADMIN or SERVERADMIN fixed server roles.
If you are new to FILESTREAM then I would recommend you read the below tips to learn more about the FILESTREAM feature.
Different ways to enable FILESTREAM feature of SQL Server 2008
Using INSERT, UPDATE and DELETE to manage SQL Server FILESTREAM Data
How to Detach and Attach a SQL Server FILESTREAM Enabled Database
Best Practices When Using FILESTREAM Feature of SQL Server 2008
5. In the below snippet you can see the files which were created within the C:\DemoFileTable\DemoFileTable_File folder when the FILESTREAM enabled database is created along with the FileTable DemoFileTableFiles. The filestream.hdr is a very important system file which basically contains FILESTREAM header information. Database Administrators need to make sure that this file is not removed or modified as this will corrupt the FILESTREAM enabled database.
6. Once the FileTable is created successfully you can access the FileTable using Windows Explorer. The path to access the FileTable will be:
\\SERVERNAME\FILESTREAM_WINDOWS_SHARE_NAME\FILESTREAM_TABLE_NAME\FILETABLE_DIRECTORY\
\\SERVERNAME\FILESTREAM_WINDOWS_SHARE_NAME\FILESTREAM_TABLE_NAME\FILETABLE_DIRECTORY\
In the below snippet you can see that I have copied MSSQLTIPS.gif logo to FileTable folder. To open the image file double click the MSSQLTips.gif file and it will open in Internet Explorer.
Finally disable the XP_CMDSHELL feature which was enabled for this demo by executing the below mentioned TSQL code.
ProblemThe FileTable feature of SQL Server 2012 is an enhancement to the FILESTREAM feature which was introduced in SQL Server 2008. In this tip we will take a look at how to use FileTable feature of SQL Server 2012.
SolutionA FileTable is a new user table which gets created within a FILESTREAM enabled database. Using the FileTable feature, organizations can now store files and documents within a special table in SQL Server and they will have the ability to access those files and documents from windows. When you use this feature it will appear to you as if the files and documents are residing on a file system rather than in SQL Server. However, in order to use the FileTable feature you need to enable the FILESTREAM feature on the instance of SQL Server 2012. Follow the steps mentioned in this tip, Different ways to enable FILESTREAM feature of SQL Server 2008, to enable the FILESTREAM feature on SQL Server 2012. Database administrators can define indexes, constraints and triggers; however the columns and system defined constrains cannot be altered or dropped. Also, in order to enable the FILESTREAM feature you need to be a member of the SYSADMIN or SERVERADMIN fixed server roles.
If you are new to FILESTREAM then I would recommend you read the below tips to learn more about the FILESTREAM feature.
Different ways to enable FILESTREAM feature of SQL Server 2008
Using INSERT, UPDATE and DELETE to manage SQL Server FILESTREAM Data
How to Detach and Attach a SQL Server FILESTREAM Enabled Database
Best Practices When Using FILESTREAM Feature of SQL Server 2008
Steps to Setup
1. Execute the below mentioned TSQL code to enabling the XP_CMDSHELL feature on SQL Server 2012. Once XP_CMDSHELL feature is enabled it will create a folder on the C: drive to store the FILESTREAM data (note: you can use any drive, but I am using the C: drive for this example).USE master GO EXEC sp_configure 'show advanced options', 1; GO RECONFIGURE; GO EXEC sp_configure 'xp_cmdshell', 1; GO RECONFIGURE; GO EXEC xp_cmdshell 'IF NOT EXIST C:\DemoFileTable MKDIR C:\DemoFileTable'; GO2. Create a database named DemoFileTable which uses the FILESTREAM feature for the purpose of the demo using the below mentioned TSQL code. In the below script you can see that we are specifying new options for the FILESTREAM clause i.e. "NON_TRANSACTED_ACCESS = FULL" and we have also provided the windows directory name "DemoFileTable" which we created in the previous step.
IF EXISTS (SELECT 1 FROM sys.databases WHERE name = 'DemoFileTable') BEGIN ALTER DATABASE DemoFileTable SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE DemoFileTable; END; CREATE DATABASE DemoFileTable WITH FILESTREAM ( NON_TRANSACTED_ACCESS = FULL, DIRECTORY_NAME = N'DemoFileTable' ); GO /* Add a FileGroup that can be used for FILESTREAM */ ALTER DATABASE DemoFileTable ADD FILEGROUP DemoFileTable_FG CONTAINS FILESTREAM; GO /* Add the folder that needs to be used for the FILESTREAM filegroup. */ ALTER DATABASE DemoFileTable ADD FILE ( NAME= 'DemoFileTable_File', FILENAME = 'C:\DemoFileTable\DemoFileTable_File' ) TO FILEGROUP DemoFileTable_FG; GO3. Next will be to Create a FileTable within FILESTREAM enabled database. This can be done by executing the below mentioned TSQL script which will create a FileTable within the FILESTREAM enabled database. The name of the FileTable is DemoFileTable and you need to specify FILETABLE_DIRECTORY as DemoFileTableFiles and FILETABLE_COLLATE_FILENAME as database_default
USE DemoFileTable; GO /* Create a FileTable */ CREATE TABLE DemoFileTable AS FILETABLE WITH ( FILETABLE_DIRECTORY = 'DemoFileTableFiles', FILETABLE_COLLATE_FILENAME = database_default ); GO4. Once the FileTable is created successfully, in Object Explorer > Expand Databases > Expand DemoFileTable database > Expand Tables > Expand FileTables > Expand dbo.DemoFileTable > Expand Columns to view the structure of FileTable as shown below.
5. In the below snippet you can see the files which were created within the C:\DemoFileTable\DemoFileTable_File folder when the FILESTREAM enabled database is created along with the FileTable DemoFileTableFiles. The filestream.hdr is a very important system file which basically contains FILESTREAM header information. Database Administrators need to make sure that this file is not removed or modified as this will corrupt the FILESTREAM enabled database.
6. Once the FileTable is created successfully you can access the FileTable using Windows Explorer. The path to access the FileTable will be:
\\SERVERNAME\FILESTREAM_WINDOWS_SHARE_NAME\FILESTREAM_TABLE_NAME\FILETABLE_DIRECTORY\
Copying Documents and Files to the FileTable
Now that we have created a FILESTREAM enabled database and a FileTable the next step will be to copy the documents and files to the newly created FileTable in Windows Explorer. You can copy the files by dragging files or by using the Copy-and-Paste operation to the below mentioned location.\\SERVERNAME\FILESTREAM_WINDOWS_SHARE_NAME\FILESTREAM_TABLE_NAME\FILETABLE_DIRECTORY\
In the below snippet you can see that I have copied MSSQLTIPS.gif logo to FileTable folder. To open the image file double click the MSSQLTips.gif file and it will open in Internet Explorer.
How to View Documents and Files Stored in FileTable Using SQL Server Management Studio
To view the files and documents stored in a FileTable execute the below mentioned TSQL code.Use DemoFileTable; GO SELECT * FROM DemoFileTable; GO
Finally disable the XP_CMDSHELL feature which was enabled for this demo by executing the below mentioned TSQL code.
USE master GO EXEC sp_configure 'show advanced options', 1; GO RECONFIGURE; GO EXEC sp_configure 'xp_cmdshell', 0; GO RECONFIGURE; GO
相关文章推荐
- SQL Server 2012的FileStream和FileTable介绍
- 系统升级日记(1)- 升级到SQL Server 2012
- Sql Server 2012 存储过程的单步调试
- SQL Server 2012 扩展事件(2)体系结构
- SQL SERVER 2012 POWER VIEW - 安装
- SQL SERVER 2012 SSIS 之 POWERSHELL
- SQL SERVER 2012 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。 (System.Data)
- SQL Server 2012笔记分享-11:理解数据变更捕获
- SQL Server 2012 各版本功能比较
- SQL Server 2012 AlwaysOn高可用配置之八:新建可用性组
- 从sql server 2012生成带数据脚本
- 使用SQL Server 2008的FILESTREAM特性管理文件(二)
- 升级到SQL Server 2012-2014时一些需要考虑的事项
- SQL Server 2012安装前准备事项
- SQL Server 2012 新的分页函数 OFFSET & FETCH NEXT
- SQL Server 2012 复制(发布订阅的研究)
- 基于Windows Server 2008 R2的WSFC实现 SQL Server 2012高可用性组(AlwaysOn Group) 推荐
- sql server 2012 备份集中的数据库备份与现有的xxx数据库不同
- SharePoint 2010 SP1 支持 SQL Server 2012
- SQL Server 2012中自带的SSDT已经废除了多维数据集的浏览功能