SET_SYS_CONTEXT

functionsupportedAccounts, users, sessions, warehouses

docs.snowflake.com ↗

Context functions

Sets 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

NameRequiredDescription
'namespace'requiredThe 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'requiredThe name of the attribute or variable to set. All key names are case-sensitive.
'value'requiredThe 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

Access control requirements

No special privileges are required to set custom session attributes. Any user can set attributes in their own session.

See also