Sql-server – EXEC xp_dirtree

extended-stored-procedurems accesspermissionssql server

I developed a stored procedure with a call to this extended SP

EXEC xp_dirtree @path, 1, 1 

Database: SQL Server 2008 R2 Express

I run the stored procedure from different sources:

  • ASP. Net with user dbo
  • MS Access with user dbo
  • MS Access with user test
  • SQL Server Management Studio with user dbo
  • SQL Server Management Studio with user test

All are OK except:

  • MS Access with user test

In this case, the SP cannot find files inside the folder.

Despite that I have no problems in:

  • MS Access with user dbo (same app)
  • SQL Server Management Studio with user test (same user)

The code from MS Access is:

Dim qdef As DAO.QueryDef
Set qdef = CurrentDb.CreateQueryDef("")
qdef.ReturnsRecords = False
qdef.Connect = CurrentDb.TableDefs("[dbo_Estados]").Connect
Dim sql As String
sql = "EXEC EnviarMails"
qdef.sql = sql
qdef.Execute

Any help? Thanks!

Best Answer

The undocumented Stored Procedure xp_dirtree requires the sysadmin fixed server role in order to see any files when the procedure is called.

Adding this role to your user test should enable you to see the results.

It is also worth repeating what has been mentioned in the comments; As this is an undocumented feature of SQL Server it could be removed in a future version and an alternative method of performing this should be used for production workloads.