I don't want to grant the user Sysadmin role. Is there any way to run a specific stored procedure as Sysadmin or as a user that is sysadmin? The stored procedure is in MSSQL 2005 and has xp_cmdshell Below is the code:
ALTER PROCEDURE [dbo].[procExcelQuotebyItem]
(
@OrderNumber INT
)
AS
BEGIN
SET NOCOUNT ON
DECLARE @Cmd varchar(1000)
DECLARE @fn varchar(500)
DECLARE @provider varchar(100)
DECLARE @ExcelString varchar(100)
-- New File Name to be created
SET @fn = 'D:\Pre-Manufacturing\Excel\QuotebyItem.xls'
/*Cleanup*/
SET @Cmd = 'DEL ' + @fn
EXEC xp_cmdshell @Cmd, no_output
-- FileCopy command string formation
SET @Cmd = 'Copy D:\Pre-Manufacturing\Excel\QuotebyItemTemplate.xls ' + @fn
-- FileCopy command execution through Shell Command
EXEC MASTER..XP_CMDSHELL @cmd, NO_OUTPUT
-- Mentioning the excel destination filename
SET @provider = 'Microsoft.Jet.OLEDB.4.0'
SET @ExcelString = 'Excel 8.0;Database=' + @fn
EXEC('INSERT INTO OPENROWSET(''' + @provider + ''',''' + @ExcelString + ''',''SELECT * FROM [Sheet1$A2:L2]'')
SELECT [ITEMNUMBER],'''',[ITEM_DESCRIPTION],[CASEPACK],[UNIT PRICE],[CASE PRICE],[WEIGHT],[CUBE],[CASE DIMS],[UPC],[CASE UPC],[Q Comments] FROM [ORDER SUMMERY] WHERE [Order #] = ''' + @OrderNumber + '''')
ALTER PROCEDURE dbo.usp_Demo
WITH EXECUTE AS 'USER_ID_with_sysadmin_rights'
AS
http://msdn.microsoft.com/en-us/library/ms188354(v=SQL.90).aspx
Test code
CREATE PROCEDURE dbo.test1
WITH EXECUTE AS 'CORP\jbooth'
AS
select user_name()
GO
EXEC test1
GO
ALTER PROCEDURE dbo.test1
WITH EXECUTE AS 'dbo'
AS
select user_name()
GO
EXEC test1
Additional info for gettting XP_CMDSHELL to work in SQL 2005