Below code is in a stored procedure and gives the following error when run under user: premfguser. Access to the remote server is denied because the current security context is not trusted.
SET @provider = 'Microsoft.Jet.OLEDB.4.0'
SET @ExcelString = 'Excel 8.0;Database=' + @fn
EXEC('INSERT INTO OPENROWSET(''' + @provider + ''',''' + @ExcelString + ''',''SELECT * FROM [Sheet1$A2开发者_JAVA技巧: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 + '''')
When I add premfguser to sysadmin it works. How can I run this code without permanently giving sysadmin to premfguser?
Use EXECUTE AS and sign the procedure with a certificate. See this example for how to sign a procedure, see also this article Signing Procedures with Certificates and this Tutorial: Signing Stored Procedures with a Certificate.
Note that the alternative of marking the database TRUSTWORTHY means that the dbo
of the database is a de-facto sysadmin
, as he can escalate himself to sysadmin anytime he likes if the trustworthy bit is set. Code signing is a much better alternative, although significantly more complex.
When you define the stored procedure, add the EXECUTE AS
clause:
CREATE PROCEDURE SomeProcedure
WITH EXECUTE AS 'UserName'
AS BEGIN
-- procedure code as usual...
END;
精彩评论