SQL Flow Field Guide

EXECUTE ... AS { LOGIN | USER }

Catalog support for SQL Server 2022 (16.x) and 2025 (17.x)

runs the string under another principal

What changes

Input
principal name
Output or change
execution context for the dynamic statement

Syntax at a glance

EXEC ( ... ) AS { LOGIN | USER } = 'name'

Work example

A controlled module must run under a reviewed database or login security context.

Test the context switch with a nonprivileged account. Define the REVERT path before the context change begins.

Check your work EXECUTE ... AS { LOGIN | USER }. The module sees only the approved permissions and the caller context is restored on every exit path.

Open Microsoft Learn for full syntax, permissions, and details for your version.