互联网时代数据是爆炸式增长,我们常常需要把结构化数据和非结构化数据(如文档,演示文稿,视频,音频,图像)存储在一起。通常有几种方案:
为了使用这项新功能,我们首先需要在实例级别启用它(在安装过程中,或通过修改SQL Server实例的属性,或通过使用sp_configure来更改实例属性),然后创建或修改数据库,有一个文件组有FileStream属性,然后创建一个表带有varbinary(max)数据类型列,并指定FileStream属性。 具体请参考 http://www.fengfly.com/plus/view-210265-1.html
启用FileStreamUSE master GO EXEC sp_configure 'filestream access level', 2 Go RECONFIGURE GO --You can use this statement to see current --config value and running value EXEC sp_configure filestream_access_level; GO
创建LearnFileTable数据库USE master GO IF EXISTS (SELECT name FROM sys.databases WHERE name = N'LearnFileTable') DROP DATABASE LearnFileTable GO CREATE DATABASE LearnFileTable --Details of primary file group ON PRIMARY ( NAME = LearnFileTable_Primary, FILENAME =N'D:\FileTable\LearnFileTable_Data.mdf', SIZE = 10MB, MAXSIZE = 50MB, FILEGROWTH = 5MB), --Details of additional filegroup to be used to store data FILEGROUP DataGroup ( NAME = LearnFileTable_Data, FILENAME =N'D:\FileTable\LearnFileTable_Data.ndf', SIZE = 10MB, MAXSIZE = 50MB, FILEGROWTH = 5MB), --Details of special filegroup to be used to store FILESTREAM data FILEGROUP FSDataGroup CONTAINS FILESTREAM ( NAME = FileStream, --FILENAME refers to the path and not to the actual file name. It --creates a folder which contains a filestream.hdr file and --also a folder $FSLOG folder as depicted in image below FILENAME =N'D:\FileTable\FSData') --Details of log file LOG ON (Name = LearnFileTable_Log, FILENAME = 'D:\FileTable\LearnFileTable_Log.ldf', SIZE = 5MB, MAXSIZE = 25MB, FILEGROWTH = 5MB ) WITH FILESTREAM (NON_TRANSACTED_ACCESS = FULL, DIRECTORY_NAME = N'LearnFileTable') --Other option for NON_TRANSACTED_ACCESS is READ_ONLY or OFF GO
检查FileStream/FileTable-- Check the Filestream/FileTable Options SELECT DB_NAME(database_id), non_transacted_access, non_transacted_access_desc, directory_name FROM sys.database_filestream_options WHERE DB_NAME(database_id) = 'LearnFileTable'创建FileTableUSE LearnFileTable GO CREATE TABLE MyFirstFileTable AS FileTable WITH ( FileTable_Directory = 'MyFirstFileTable', FileTable_Collate_Filename = database_default ); GO如果我们创建FileTable前没有启用FileStream,会报错
Msg 1969, Level 16, State 1, Line 1 Default FILESTREAM filegroup is not available in database '<database_name>'
创建后,我们查询一下,没有记录
USE LearnFileTableSELECT * FROM [dbo].[MyFirstFileTable]在企业管理器选择FileTable,右键 "Explorer FileTable Directory" link as shown below:
你会看到网络共享目录. 手动添加几个文件到该目录,我们返回SQLServer企业管理器看看有什么事情发生:再运行一次查询语句
USE LearnFileTableSELECT * FROM [dbo].[MyFirstFileTable]