SYS_CONTEXT

SYS_CONTEXT is a function in Oracle PL/SQL that helps in accessing parameter values from context set for a session. It retrieves the value of a parameter associated with a specific context. The context information helps to control the behavior of applications, for tailored security, global application information tracking and more.

SYS_CONTEXT(‘namespace_string’, ‘parameter_string’)

  • ‘namespace_string’: This parameter specifies the namespace. A namespace is an environment or domain that the user-defined parameter belongs to. SYS_CONTEXT requires ‘namespace_string’ to evaluate user-defined parameters.
  • ‘parameter_string’: This parameter specifies the parameter, the attribute within the namespace you are looking to retrieve. These parameters could be predefined by the system or user-defined. The ‘parameter_string’ within SYS_CONTEXT will return the value of the specified parameter.

Example

SELECT SYS_CONTEXT('USERENV', 'SESSION_USER') FROM DUAL;

Output

SYS_CONTEXT('USERENV','SESSION_USER')
------------------------------------
SCOTT

Explanation

The SYS_CONTEXT function is a very flexible function in Oracle that allows you to retrieve information about the Oracle environment. The above example shows how to use the SYS_CONTEXT function to get the current session user. The 'USERENV' parameter is a built-in namespace in Oracle that includes a variety of useful information about the user and session context. The 'SESSION_USER' attribute represents the name of the user that initiated the current session.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.