适用于:SQL Server
Azure SQL 数据库
Azure SQL 托管实例
Azure Synapse Analytics
将执行上下文切换回最后 EXECUTE AS 一个语句的调用者。
语法
REVERT
[ WITH COOKIE = @varbinary_variable ]
参数
WITH COOKIE = @varbinary_variable
在对应 EXECUTE AS 的独立语句中指定创建的Cookie。 @varbinary_variable 是 varbinary(100)。
注解
REVERT 可以作为模块内指定,如存储过程或用户自定义函数,也可以作为独立语句。 在模块内指定时, REVERT 仅 EXECUTE AS 适用于模块中定义的语句。 例如,以下存储过程发出后跟 EXECUTE AS 语句的 REVERT 语句。
CREATE PROCEDURE dbo.usp_myproc
WITH EXECUTE AS CALLER
AS
SELECT SUSER_NAME(), USER_NAME();
EXECUTE AS USER = 'guest';
SELECT SUSER_NAME(), USER_NAME();
REVERT;
SELECT SUSER_NAME(), USER_NAME();
GO
假定在运行存储过程的会话中,会话的执行上下文显式更改为 login1,如下例所示。
-- Sets the execution context of the session to 'login1'.
EXECUTE AS LOGIN = 'login1';
GO
EXECUTE dbo.usp_myproc;
在 REVERT 内部定义的 usp_myproc 语句会切换在模块内部设置的执行上下文,但不会影响在模块外部设置的执行上下文。 就是说,会话的执行上下文将仍然设置为 login1。
当被指定为独立语句时, REVERT 适用于 EXECUTE AS 批处理或会话中定义的语句。 REVERT 如果对应 EXECUTE AS 语句包含 WITH NO REVERT 子句,则无效。 在这种情况下,执行上下文将保持有效状态,直到删除会话。
使用 REVERT WITH COOKIE
用于设置会话执行上下文的 EXECUTE AS 语句可以包含可选子句 WITH NO REVERT COOKIE = @varbinary_variable。 运行此语句时,数据库引擎 将 cookie 传递给 @varbinary_variable。 该语句所设定的执行上下文只有在调用 REVERT WITH COOKIE = @varbinary_variable 语句包含正确的 @varbinary_variable 值时,才能恢复到之前的上下文。
此机制在使用连接池的环境中是有用的。 连接池是一组供跨越多个最终用户的应用程序重用的数据库连接的维护机制。 由于传递给 @varbinary_variable 的值只有调用该语句的调用者 EXECUTE AS (此例中是应用程序)知道,调用者可以保证他们建立的执行上下文不会被调用应用的终端用户更改。 恢复执行上下文之后,应用程序可以将上下文切换到另一个主体。
权限
不需要任何权限。
示例
A. 使用 EXECUTE AS 和 REVERT 切换上下文
以下示例通过使用多个主体创建上下文执行堆栈。 然后使用 REVERT 语句将执行上下文重置为上一个调用方。 将多次执行 REVERT 语句以向上移动堆栈,直到将执行上下文设置为原始调用方为止。
USE AdventureWorks2022;
GO
-- Create two temporary principals.
CREATE LOGIN login1 WITH PASSWORD = 'J345#$)thb';
CREATE LOGIN login2 WITH PASSWORD = 'Uor80$23b';
GO
CREATE USER user1 FOR LOGIN login1;
CREATE USER user2 FOR LOGIN login2;
GO
-- Give IMPERSONATE permissions on user2 to user1
-- so that user1 can successfully set the execution context to user2.
GRANT IMPERSONATE ON USER:: user2 TO user1;
GO
-- Display current execution context.
SELECT SUSER_NAME(), USER_NAME();
-- Set the execution context to login1.
EXECUTE AS LOGIN = 'login1';
-- Verify that the execution context is now login1.
SELECT SUSER_NAME(), USER_NAME();
-- Login1 sets the execution context to login2.
EXECUTE AS USER = 'user2';
-- Display current execution context.
SELECT SUSER_NAME(), USER_NAME();
-- The execution context stack now has three principals: the originating caller, login1, and login2.
-- The following REVERT statements will reset the execution context to the previous context.
REVERT;
-- Display the current execution context.
SELECT SUSER_NAME(), USER_NAME();
REVERT;
-- Display the current execution context.
SELECT SUSER_NAME(), USER_NAME();
-- Remove the temporary principals.
DROP LOGIN login1;
DROP LOGIN login2;
DROP USER user1;
DROP USER user2;
GO
B. 使用 WITH COOKIE 子句
以下示例将会话的执行上下文设置为指定用户,并指定了 WITH NO REVERT COOKIE = @varbinary_variable 子句。
REVERT 语句必须指定传递给 @cookie 语句中的 EXECUTE AS 变量的值,否则,无法成功将上下文恢复为该调用方。 若要执行此示例,必须存在示例 A 中所创建的 login1 登录名和 user1 用户。
DECLARE @cookie VARBINARY(100);
EXECUTE AS USER = 'user1' WITH COOKIE INTO @cookie;
-- Store the cookie somewhere safe in your application.
-- Verify the context switch.
SELECT SUSER_NAME(), USER_NAME();
--Display the cookie value.
SELECT @cookie;
GO
-- Use the cookie in the REVERT statement.
DECLARE @cookie VARBINARY(100);
-- Set the cookie value to the one from the SELECT @cookie statement.
SET @cookie = <value from the SELECT @cookie statement>;
REVERT WITH COOKIE = @cookie;
-- Verify the context switch reverted.
SELECT SUSER_NAME(), USER_NAME();
GO
另请参阅
EXECUTE AS (Transact-SQL)
EXECUTE AS 条款(Transact-SQL)
EXECUTE (Transact-SQL)
SUSER_NAME(Transact-SQL)
USER_NAME(Transact-SQL)
CREATE LOGIN (Transact-SQL)
CREATE USER (Transact-SQL)