EXECUTE AS OWNER specifies the statements inside the module executes in the context of the current owner of the module. If the module does not have a specified owner, the owner of the schema of the module is used.
Use EXECUTE AS OWNER in the following scenario:
-
You want to be able to change owner of the module without having to modify the module itself. That is, OWNER automatically maps to the current owner of the module at run time.
OWNER is the explicit owner of the module or, if there is not an explicit owner, the owner of the schema of the module at the time the module is executed. OWNER must be a singleton account and not a group or role. The ownership of the module cannot be changed to a group or role when the module specifies EXECUTE AS OWNER and has an explicit owner. The ownership of a schema cannot be changed to a role or group when it contains a module that specifies EXECUTE AS OWNER and the modules does not have an explicit owner.
EXECUTE AS OWNER Scenario
Mary creates a stored procedure that references a table that she owns. She specifies EXECUTE AS OWNER in the CREATE PROCEDURE statement, as shown in this example:
CREATE PROCEDURE Mary.AccessMyTable
WITH EXECUTE AS OWNER
AS SELECT * FROM Mary.MyTable;
Mary grants EXECUTE permissions on the stored procedure to Scott. When Scott executes the stored procedure, the Database Engine verifies that he has permission to execute the stored procedure; however, permissions for the referenced table are checked for Mary because she is the current owner of the procedure. Mary decides to leave the company and changes ownership of the procedure and table to Tom. When Scott executes the stored procedure after the ownership change, he is still able to access the data through the procedure because OWNER is automatically mapped to Tom.