Please help me on below query .

NOTE : Please excuse my programming skills , the code is of mistakes .

I want the output like Database name in one column and the FileStream enabled or disabled in another column . Just 2 columns I am trying out for . Thanks

To find the Database name and if FileStream is enabled for the Database .

declare @cmd varchar(500)
set @cmd='USE ? 
if exists(
 select name, Filestream from sys.database_files where type_desc='FILESTREAM')
 CASE type_desc WHEN 'FILESTREAM' THEN 'Filestream Configured For Datbase' ELSE 'Filestream NOT Configured For Datbase'
PRINT 'Filestream Configured For Datbase'
 print 'Filestream Not configured for database' 
inner join sys.databases where name not in (''master'', ''tempdb'', ''model'', ''msdb'')')
exec sp_MSforeachdb @cmd 

2 Answers 2



I would rephrase you question to find databases that are using FILESTREAM. Because you can only enable FILESTREAM at an instance level in order to use it in any user database.

To find all the files related to FILESTREAM or database name you can use this query.

SELECT db.NAME       AS DBName, 
       type_desc     AS FileType, 
       physical_name AS Location 
FROM   sys.master_files mf 
       INNER JOIN sys.databases db 
               ON db.database_id = mf.database_id 
WHERE  type_desc = 'FILESTREAM' 

Due to the fact that sp_MSforeachdb has known problems, I opted for Aaron Bertrand's sp_foreachdb stored procedure to query each database. I modified one of my local databases (Test) to enable FILESTREAM to see if my query returned the correct results (which it did in my case) - see if this returns the information you want.

--Temp table to hold the results
IF OBJECT_ID('tempdb..#Results') IS NOT NULL
    DROP TABLE [dbo].[#Results];

CREATE TABLE [dbo].[#Results] (
    ,[FileStreamDescription] VARCHAR(max) NOT NULL

--Insert into #Results for each database
execute master.dbo.sp_foreachdb 'INSERT INTO #Results SELECT ''?'' as DBNAME,
        sys.database_filestream_options dfo join 
        sys.databases d 
            on dfo.database_id = d.database_id 
            WHERE QUOTENAME(d.name) = ''?'' and dfo.NON_TRANSACTED_ACCESS <> 0) 
        THEN ''Filestream Configured For Database'' 
        ELSE ''Filestream NOT Configured For Datbase'' 
where ''?'' NOT in (''[master]'', ''[tempdb]'', ''[model]'', ''[msdb]'')

--Query the #Results table
SELECT * FROM #Results order by DBNAME;

 DBNAME               | FileStreamDescription                 |
| [AdventureWorks2014] | Filestream NOT Configured For Datbase |
| [foo]                | Filestream NOT Configured For Datbase |
| [ReadingDBLog]       | Filestream NOT Configured For Datbase |
| [Test]               | Filestream Configured For Database    |
| [zzz]                | Filestream NOT Configured For Datbase |
  • Thanks a lot @Scott . It is very informative . Thanks a lot . Commented Jan 4, 2018 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.