SQL: EXEC AS USER on EXEC Statements
The WITH EXECUTE AS clause was a great addition for defining stored procedures and functions, to change the execution context, just for the duration of the stored procedure or function. For example:
CREATE PROC SomeSchema.SomeProc
WITH EXECUTE AS USER = 'Fred'
AS
...
Mostly I use this with the OWNER option:
CREATE PROC SomeSchema.SomeProc
WITH EXECUTE AS OWNER
AS
...
It’s also useful during testing, where I can temporarily change my execution context during testing. For example:
EXEC AS USER = 'Fred';
\-- Try some code here while running as Fred
REVERT;
But the option that most people don’t realize is possible, is that you can set the execution context for a single execution like this:
2026-04-10
