Is there a way to find out if SQL Server databases are attached from a directory with many subfolders apart from going to properties of each .mdf and checking if it is attached?
Is there a script that can check folders/subfolders an write to file to accomplish this?
THanks all.
When you need to read the file system from T-SQL, I find that CLRs have the most flexibility. They give you full access to the system.io namespace as well as support for table valued functions.
Have a look at the SQL Server CLR IO Utility. There is a full solution here which enables filesystem functionality that is not easily available with the frowned upon xp_cmdshell.
The table valued function, SQLIO_fnGetFiles, can be used to join to the sys.master_files table and return files which are no longer attached to a database. In this example, the CLR is deployed to a database named CLRS:
There are also other methods (powershell, .net, ssis,…) to accomplish this. My experience is that once you deploy the IO Utility solution, you’ll find many other time saving uses for it.