Skip to main content

SQL Server(from 2012) FileTable Features some useful SQL Statements

The FileTable feature brings support for the Windows file namespace and compatibility with Windows applications to the file data stored in SQL Server. FileTable lets an application integrate its storage and data management components, and provides integrated SQL Server services - including full-text search and semantic search - over unstructured data and metadata.(FileTable)

Here is sequential SQL statements to enable,alter existing database for FileStream and create FileTable

--check current config value
EXEC sp_configure filestream_access_level;

--reconfig
GO
EXEC sp_configure 'filestream access level',   2
Go
RECONFIGURE
GO

-- chack file table enabled databases 
SELECT DB_NAME(database_id) as [Database Name],
non_transacted_access,
non_transacted_access_desc
,directory_name
FROM sys.database_filestream_options


--  Enable a file table in an existing Database
ALTER DATABASE TestDB
 SET FILESTREAM ( NON_TRANSACTED_ACCESS = FULL, DIRECTORY_NAME = N'testFileTable' )
GO
--create file table
CREATE TABLE MyFirstFileTable   AS FileTable
WITH
(
      FileTable_Directory =   'MyFirstFileTable',
      FileTable_Collate_Filename = database_default
);
GO
-- DB Operations on FileTable
USE [TestDB]
GO

SELECT [stream_id]
      ,[file_stream]
      ,[name]
      ,[path_locator]
      ,[parent_path_locator]
      ,[file_type]
      ,[cached_file_size]
      ,[creation_time]
      ,[last_write_time]
      ,[last_access_time]
      ,[is_directory]
      ,[is_offline]
      ,[is_hidden]
      ,[is_readonly]
      ,[is_archive]
      ,[is_system]
      ,[is_temporary]
  FROM [dbo].[MyFirstFileTable]
GO


select FileTableRootPath() as rootPath 

--update [dbo].[MyFirstFileTable] set  [name]='Aquabella.xlsx' where [name]='Aquabella 91.xlsx'

--get root level UNC path
select FileTableRootPath() --1

select FileTableRootPath('[dbo].[MyFirstFileTable]',0) --2

select FileTableRootPath('[dbo].[MyFirstFileTable]',1) --3

--full files path 
SELECT  FileTableRootPath() + file_stream.GetFileNamespacePath()  As FullPath,[name] as [FileName]

FROM [dbo].[MyFirstFileTable]  

Hope it helps,Thanks

Comments

Popular posts from this blog

Cannot access a disposed object. A common cause of this error is disposing a context that was

  Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'PropertyContext'.  public class CustomerHub : Hub     {         public readonly ICustomerService _customerService;         private readonly IServiceScopeFactory _serviceScopeFactory;         public CustomerHub(ICustomerService service, IServiceScopeFactory serviceScopeFactory)         {             _customerService = service;             _serviceScopeFactory = serviceSco...

Limit Upload File Type Extensions ASP.NET MVC 5

  //-----------------------------------------------------------------------    // <copyright file="AllowExtensionsAttribute.cs" company="None">    //     Copyright (c) Allow to distribute this code and utilize this code for personal or commercial purpose.    // </copyright>    // <author>Asma Khalid</author>    //-----------------------------------------------------------------------       namespace  ImgExtLimit.Helper_Code.Common   {        using  System;        using  System.Collections.Generic;        using  System.ComponentModel.DataAnnotations;        using  System.Linq;      ...