Search Suggest

SQL Server, How to manage files in SQL Server. The Filetables feature

Hi Guys,

Welcome back to this my SQL server technical blog!

I am here again after three weeks of holiday that I really needed in order to recharge my internal batteries.

Now that my batteries are full I am able to hear you voices that are asking: 

What will be today's topic? Okay okay today we will speak about “How to manage file in SQL server”.

 

How to manage file in SQL server


You must know that starting from the 2012 edition SQL Server have a filetables feature.
Mmmmh…. never heard that?

As usually I will try to explain myself in the clearest way. But ask me it is not!
So are you ready? Let’s go!!!


The Filetable feature. Intro


With The FILETABLE feature (that is the next step in the FILESTREAM technology) you can store files and folders inside the special tables.

You can access to these table with the traditional Windows SMB protocol.

FileTables let us use SQL tables for non-transactional access so is possible create, read, modify or delete files from a generic file share without even being aware of the underlying SQL commands.

Important: Our special table will contain also the file attibutes such as Last_AccessTime, Last-WriteTimeand this will allow to audit the file access directly querying the corresponding columns of the FileTable.


In order to use the FILETABLE feature it must be configured and enabled. 


How to configure and enable the Filetable feature

 
First step
 
Find the tab FILESTREAM in the SQL Server properties and enable it as in the photo below.

 


Second step, From the SSMS open a new tab and execute the statements below:

       
EXEC sp_configure filestream_access_level, 2

RECONFIGURE

 

Third step


For the our example we will create a new database called Fileserver.
Note that we add the part colored in yellow to the definition of the create database command.

       

CREATE DATABASE FileServer

ON

PRIMARY ( NAME = fs1,

FILENAME = 'C:\Scambio\filestream\FileServer.mdf'),

FILEGROUP FileStreamGroup1 CONTAINS FILESTREAM( NAME = fs3,

FILENAME = 'C:\Scambio\filestream\FileServer')

LOG ON ( NAME = fslog1,

FILENAME = 'C:\Scambio\filestream\FileServer.ldf')

GO




Executed the T-SQL command you will note that we have a third datafile with datatype filestream:
 
 
 

Fourth step

Now we need to enable the non-transactional access for the database.

Once we’ve got the database created, the last step in preparing it for FileTables is to enable non-transactional access at the database level.

FileTables let Windows applications obtain a Windows file handle to FILESTREAM data without requiring a transaction.

To allow this non-transactional access to files stored in SQL Server, you have to specify the desired level of non-transactional access at the database level for each database that will contain FileTables.


       

ALTER DATABASE FILESERVER

SET FILESTREAM ( NON_TRANSACTED_ACCESS = FULL, DIRECTORY_NAME = N'FILESERVER' )

GO


 
 
 

How to use the Filetable feature, an example 

 

First of all create a new Filetable called Documenti executing the command:

       

CREATE TABLE Documenti AS FileTable;


 
Now from the explorer menu of the SSMS we will view:


We can access to our filetable simply querying it.. 

       

Select * from Documenti


 


Now files (and directories) and filetable “documenti” are synchronized
 
 
1) If you paste a file into the Documenti folder you will see a row in the documenti filetable
 
 
 
 

2) If you delete a row from the  documenti filetable (deletefrom Documenti) we will delete the file.


With this feature directly from a T-SQL command you can do many more things like….

  • Create a new folder
 
       

INSERT INTO Documenti(Name, is_directory) values('subfolders',1)


 

  • Rename files or Update attributes 
       


UPDATE [dbo].Documenti

SET

[is_readonly] = 1

,[is_archive] = 1


WHERE name='paq8px_exe.txt'



  • Insert files.. 
       

INSERT INTO [dbo].Documenti

([name],[file_stream])

SELECT

'paq8px_exe.txt',

* FROM OPENROWSET(BULK N'\\CSW011\MSSQLSERVER\FILESERVER\Documenti\paq8px_exe.txt',

SINGLE_BLOB) AS FileData

GO


 

  • Copy files from and to a directory


You can also perform auditing on the files since on the table documenti we have also file attributes:
 
 
 

Give Permissions to the users


Filetables such as any other user table have permission.

So, Defining dedicates windows user you can choose for each one of them who can insert or update the files.




That's all for Today!
Luca











Post a Comment