SQL: EXEC AS USER on EXEC Statements

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:

EXEC ('Some command to be executed as Fred') AS USER = 'Fred';

This could be particularly useful when you need to call dynamic SQL code with a specific execution context.

I hope that helps to simplify your code.

Learn more about SQL Server Administration

If you really want to learn about SQL Server administration right now, we have an online on-demand course that you can enrol in, right now. You’ll find it at SQL Server Administration for Developers and DBAs

2026-04-10