EXECUTE AS Statement
Allows to execute queries on behalf of a different user.
Syntax
The first form (without subquery) sets that all the following queries in the current session will be executed on behalf of the specified target_user.
The second form (with subquery) executes only the specified subquery on behalf of the specified target_user.
In order to work both forms require server setting allow_impersonate_user
to be set to 1 and the IMPERSONATE privilege to be granted. For example, the following commands
allow user user2 to execute commands EXECUTE AS user1 ... and also allow user user3 to execute commands as any user.
While impersonating another user function currentUser() returns the name of that other user, and function authenticatedUser() returns the name of the user who has been actually authenticated.