SET_SYS_CONTEXT
functionsupportedAccounts, users, sessions, warehousesSets a value for a specified key in a specified namespace that can be retrieved later using SYS_CONTEXT. This function has two modes of operation: - Immutable session attributes (SNOWFLAKE$SESSION_ATTRIBUTES namespace): Sets custom session attributes that are immutable once set and persist for the duration of the session. Useful for tracking metadata about a session, such as application context, user attributes, or audit information. - Session variables (other namespaces): Behaves like the SET command, setting session variables that can be updated. Returns the previous value of the variable.
Syntax
CALL SET_SYS_CONTEXT( '<namespace>', '<key>', '<value>' )
Parameters
| Name | Required | Description |
|---|---|---|
'namespace' | required | The namespace in which to store the key-value pair. Supported namespaces: SNOWFLAKE$SESSION_ATTRIBUTES — Stores immutable custom session attributes. Attribute names are case-sensitive. Any other string (or NULL) — Treats the namespace as a prefix for a session variable name, similar to the SET command. Namespaces are case-sensitive. |
'key' | required | The name of the attribute or variable to set. All key names are case-sensitive. |
'value' | required | The value to assign. The value must be a string or an expression that evaluates to a string. |
Returns
The function returns a VARCHAR value: - For the SNOWFLAKE$SESSION_ATTRIBUTES namespace: Always returns NULL (because immutable attributes cannot have a previous value). If the attribute has already been set in the current session, the function raises an error instead. - For other namespaces: Returns the previous value of the session variable, or NULL if the variable did not previously exist. This matches the behavior of the SET command. 2026_06 behavior change bundle When the 2026_06 behavior change bundle is enabled in your account, it changes the return type of reads through SYS_CONTEXT. The return value of SET_SYS_CONTEXT (the previous value of the attribute or session variable) is unaffected and continues to be VARCHAR.
Usage notes
- For SNOWFLAKE$SESSION_ATTRIBUTES namespace (immutable attributes):
- Attributes are immutable. Once an attribute is set, any attempt to set it again (even to the same value) will result in an error.
- Attribute names are case-sensitive. app_context, App_Context, and APP_CONTEXT are treated as three different attributes.
- Attributes are session-scoped. They persist for the duration of the session and are not visible to other sessions.
- To retrieve attribute values, use SYS_CONTEXT (SNOWFLAKE$SESSION_ATTRIBUTES namespace): SYS_CONTEXT('SNOWFLAKE$SESSION_ATTRIBUTES', '<key>').
- For other namespaces (session variables):
- Variable names are case-sensitive. user_id and USER_ID are treated as different variables.
- Variables can be updated. Setting a variable that already exists returns the previous value and updates it with the new value.
- The namespace (if provided) is used as a prefix: SET_SYS_CONTEXT('myns', 'mykey', 'val') creates a variable named myns.mykey.
- Variables can be retrieved using SYS_CONTEXT('<namespace>', '<key>') with the exact case used when setting the variable.
- General notes:
- If you are specifying the function call in a double-quoted string in a shell, escape the $ character with a backslash (\) so that $session_attributes is not interpreted as a shell variable.
Access control requirements
No special privileges are required to set custom session attributes. Any user can set attributes in their own session.