Unable to create stored procedure to access excel files in SQL Server? I need to create a stored procedure to access Excel files in SQL Server. I have created the below stored procedure. Since I am a sysadmin, the stored procedure is working fine for me.

But for the users who do not have SA privileges they are getting an error.

Could some one assist me on this, how the users can execute the stored procedure with my credentials (SYSADMIN)?

create procedure [dbo].[readexcel] 
    (@filepath varchar(max))
as
    declare @Statement varchar(max)
    --set @Statement = 
    EXECUTE('select * FROM OPENROWSET(' +
        '''Microsoft.ACE.OLEDB.12.0'',' +
        '''Excel 12.0;Database=' + @filepath +''',' +
        '''SELECT * FROM [Sheet1$]'')'); 


Error message:


  Ad hoc access to OLE DB provider 'Microsoft.ACE.OLEDB.12.0' has been
  denied. You must access this provider through a linked server
The good news is it CAN be done.  The bad news is you are opening a security hole.


Create a new database for this SP.
Make the owner of the database a sysadmin.
Set the database to trustworthy
Create your SP in the database using EXECUTE AS OWNER
For the people you want to be able to run this grant ONLY the following permissions.


CONNECT (create them a user)
EXECUTE on the stored procedure.



The reason for moving it off to it's own database &amp; dramatically restricting permissions there I hope is fairly obvious.  By setting things up this way anyone who can run the stored procedure is in effect a sysadmin.  Now it's restricted to the code in the SP.  But what happens if someone gets db_owner access?  They can now write SPs that can grant them sysadmin.  Note: db_owner is a simple example.  There are lower permissions that could cause problems also.