We help IT Professionals succeed at work.
Get Started

Use xp_create_subdir with Non Administrative accounts

Last Modified: 2020-03-06

I'm trying to allow non admins to use the xp_create_subdir function (Responsibly) in T-SQL 2016 environment within a stored procedure. We have a process in place where we keep billing backup (Proof of work) in an invoice folder.  Once the file is billed we need to create the next iteration of that billing number. Example for file 12345.  Once we bill 12345-1 we want to create a folder called 12345-2.

I've created a user account that will be used as a service account. This was done so rights to the folder location could be granted.  Inside the database I've created the stored procedure using "WITH EXECUTE AS OWNER" as I do not wish to grant all users the ability to use xp_create_Subdir. I’ve created a new schema just for this sp, the schema owner has been set to the service account. I’ve added the service account as the schema owner, db_Owner, and sysadmin.  Only the sysadmin permission is required, but I want to try to alleviate any suggestions I've already tried. I’ve also granted execute rights to the xp_Create_subdir function to the service account.
The execute line is fairly generic:
DECLARE @FilePath varchar(255)
SET @FilePath = ‘[File Path]’
EXEC master.dbo.xp_create_subdir @FilePath

I have run the script myself (sysadmin) and it works.  However as soon as it’s put in a stored procedure it fails with error:
Msg 22048, Level 16, State 1, Line 2
Error executing 'xp_create_subdir': Permission denied. User must be a member of 'sysadmin' server role.

I’ve also tried adding myself (sysadmin) as the owner of the stored procedure, schema, and database, then executed the stored procedure under my sysadmin login, and get the same error.  What am I missing?
Watch Question
Senior DBA
Most Valuable Expert 2018
Distinguished Expert 2019
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE