Using T-SQL to Query a File System Folder

2019-06-19 00:46发布

问题:

Is it possible to query a folder in TSQL, from SQL Management Studio, and return a list of file names? If so, how?

回答1:

You can use xp_cmdshell.

Example:

EXECUTE master.dbo.xp_cmdshell 'DIR "C:\YourDirectory\" /A-D /B'

There's a good, full example with more options here.



回答2:

CLR integration is also an option, if you're not comfortable with allowing xp_cmdshell to be executed.

http://msdn.microsoft.com/en-us/library/ms345136%28SQL.90%29.aspx