Can you set a stored procedure to always execute as a specific user?

假装没事ソ 提交于 2019-12-24 07:07:19

问题


I would like to set a stored procedure to always execute as a specific domain user. Regardless of the user calling or trying to execute the procedure, can I force the procedure to be executed as another user.

One of our vendors has their app hardcoded to use a local db account to execute certain procedures to import some csv files. Unfortunately we cannot be storing the csv files on the local database server and need to place them on a network share. Therefore I need to set these procedures to be executed as a domain user with access to the network share where I will be placing the files. The below statements grant the execute permissions but I need something that forces the sp to be executed as a specific user.

CREATE ROLE exec_procs
GRANT EXECUTE ON sys.sp_oaMETHOD TO [domain\user]

回答1:


You want to use EXECUTE AS in the SP definition.

CREATE PROCEDURE dbo.MyProcedure
WITH EXECUTE AS 'domain\user'
AS
...

Alternatively, you can execute only certain commands as the user within the SP:

EXECUTE AS USER = 'Domain\User'
  <Commands>
REVERT

The second option may be preferable in your situation to prevent giving another login access to the database.



来源:https://stackoverflow.com/questions/22260929/can-you-set-a-stored-procedure-to-always-execute-as-a-specific-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!