I have these two stored procedures, and Sales.spExec1
calls dbo.spExec2
.
When I try to REVERT in the 2nd stored procedure, it doesn't go back to original_login()
.
Does this mean that REVERT
works within a stored procedure? It does revert when I call it in Sales.spExec1
.
CREATE PROCEDURE Sales.spExec1
AS
BEGIN
PRINT SUSER_SNAME() -- 'MyDomain\Bob.Smith'
PRINT original_login() -- 'MyDomain\Bob.Smith'
EXECUTE AS USER = 'TempUser';
EXEC dbo.spExec2
REVERT;
/* REVERT works in SP where initial EXECUTE AS USER ran */
print SUSER_SNAME() -- 'MyDomain\Bob.Smith'
print original_login() -- 'MyDomain\Bob.Smith'
END;
GO
CREATE PROCEDURE dbo.spExec2
AS
BEGIN
PRINT SUSER_SNAME() -- 'TempUser'
PRINT original_login() -- 'MyDomain\Bob.Smith'
REVERT;
/* REVERT did not work */
print SUSER_SNAME() -- Still `TempUser'
print original_login()
END;
GO
1条答案
按热度按时间gopyfrb31#
This is documented here:
REVERT can be specified within a module such as a stored procedure or user-defined function, or as a stand-alone statement. When specified inside a module, REVERT is applicable only to EXECUTE AS statements defined in the module.
REVERT (Transact-SQL)
Even a dynamic SQL nested batch doesn't allow REVERT to change the current identity that the batch was started with, eg
outputs