title: How to run a Stored Procedure as Sysadmin?
body: <p>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:</p>
  
  <pre><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 + '''')
  </code></pre>