REVERT

REVERT is a Transact-SQL statement in Microsoft SQL Server. It is used to discard the context of the current user and revert back to the context of the user who either called the current user or, if there are no calling users, the original login. This means that REVERT undoes the effect of an EXECUTE AS statement, eliminating any impersonation of another user. This statement does not require any parameters.

Example

-- Assume there is an existing connection with CONTROL SERVER
EXECUTE AS USER = 'CompanyUser01';
SELECT SUSER_NAME();
REVERT;
SELECT SUSER_NAME();

Output

CompanyUser01
sa (or a different login with CONTROL SERVER)

Explanation

The EXECUTE AS USER statement is used to change the execution context to the specific user ‘CompanyUser01’. The SUSER_NAME() function is then used to display the name of the user currently being utilized. Following this, the REVERT command is used to revert the context back to the prior user, and finally SUSER_NAME() function is used once more to confirm that the context has been reverted.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.