Read End-User Context Attributes

Use the ORA_END_USER_CONTEXT function to retrieve end-user context attributes from the current end-user security context.

This function returns a JSON object containing all end-user context attributes for the current security context. You can use dot notation to navigate the JSON hierarchy. Depending on the provided field parameters, the function can return a single attribute value, all attributes within a specific end-user context, or all attributes across all instantiated end-user contexts.

In addition to the ORA_END_USER_CONTEXT function, the SYS.END_USER_CONTEXT view exposes all instantiated end-user contexts and their attributes in a table format, with each row representing one context’s content as a JSON document. This view is the primary interface for querying or updating end-user context attributes using standard DML syntax.

Required privilege

Syntax

ORA_END_USER_CONTEXT[.schema[.name[.attribute]]]

Parameters

Parameter Description
schema The owner of the end-user context. Omit for USER.DEFAULT system attributes (which are elevated to the top level).
name The name of the end-user context. If omitted, all end-user contexts under the schema are returned.
attribute The specific attribute within the end-user context. If omitted, the entire end-user context is returned as a JSON object.

Usage notes and restrictions

For syntax diagrams and additional details, see ORA_END_USER_CONTEXT in Oracle AI Database SQL Language Reference.

Example: Return all available context attributes for the current session

SELECT ORA_END_USER_CONTEXT FROM dual;

If hr.hcm_context has been instantiated with emp_id = 3 and org_id = 5, the above statement returns a JSON document similar to the following example:

{
    "USERNAME": "SCOTT",
    "LOGON_END_USER": "SCOTT",
    "CURRENT_END_USER": "SCOTT",
    "USER": {
        "TOKEN": {
            "TOKEN_ID": "...",
            ...
        }
    },
    "HR": {
        "HCM_CONTEXT": {
            "emp_id": 3,
            "org_id": 5
        }
    }
}

Example: Return all attributes for a specific context

Return all attributes for hr.hcm_context.

SELECT ORA_END_USER_CONTEXT.hr.hcm_context FROM dual;

Output:

{
    "emp_id": 3,
    "org_id": 5
}

Example: Return all contexts under a schema

Return all contexts under the hr schema.

SELECT ORA_END_USER_CONTEXT.hr FROM dual;

Example: Return a specific attribute value

Return the emp_id attribute from hr.hcm_context.

SELECT ORA_END_USER_CONTEXT.hr.hcm_context.emp_id FROM dual;

Example: Access system-level attributes

SELECT ORA_END_USER_CONTEXT.username FROM dual;
SELECT ORA_END_USER_CONTEXT.DB_NAME FROM dual;
SELECT ORA_END_USER_CONTEXT.USER.TOKEN.iss FROM dual;

Example: Query a specific attribute using the SYS.END_USER_CONTEXT view

Use the SYS.END_USER_CONTEXT view to query a specific attribute using the standard table syntax.

SELECT t.CONTEXT.org_id
FROM   SYS.END_USER_CONTEXT t
WHERE  owner = 'HR' AND name = 'HCM_CONTEXT';

This is equivalent to the following command:

SELECT ORA_END_USER_CONTEXT.hr.hcm_context.org_id FROM dual;

Example: Use a context attribute in a query predicate

Use a subquery against the SYS.END_USER_CONTEXT view in a predicate.

SELECT * FROM hr.employees
WHERE  employee_id  = (
    SELECT t.CONTEXT.emp_id
    FROM   SYS.END_USER_CONTEXT t
    WHERE  owner = 'HR' AND name = 'HCM_CONTEXT'
);

Example: Use a context attribute in a data grant predicate

CREATE DATA GRANT hcm_dg AS
  SELECT ON hr.employees
  WHERE employee_id  = ORA_END_USER_CONTEXT.hr.hcm_context.emp_id
  TO john;