Impersonation
Impersonation allows service accounts and administrators to execute SQL statements on behalf of another user. This is essential for bot interactions and service-side operations.
EXECUTE AS USER
EXECUTE AS USER '<username>' (
<single_statement>
);Rules
- The wrapper must contain exactly one SQL statement
- The username must be single-quoted
- Legacy inline
... AS USER 'name'syntax is not supported
Examples
Query as a specific user:
EXECUTE AS USER 'admin' (
SELECT * FROM app.messages WHERE conversation_id = 42
);Insert on behalf of a service bot:
EXECUTE AS USER 'service_bot' (
INSERT INTO app.messages (conversation_id, sender, role, content)
VALUES (42, 'service_bot', 'assistant', 'Processing complete')
);Delete as admin:
EXECUTE AS USER 'admin' (
DELETE FROM app.messages WHERE id = 123
);Use Cases
| Scenario | Description |
|---|---|
| AI bot responses | Service account inserts assistant messages into user conversations |
| Admin operations | DBA performs maintenance on user tables |
| Testing | Simulate user actions in development |
| Audit trails | Actions are logged under the impersonated user |
Last updated on