Oracle SYS_CONTEXT() Function

Oracle SYS_CONTEXT() is a built-in function that returns the value of the given parameter associated with the given context at the current time.

Oracle SYS_CONTEXT() Syntax

Here’s the syntax for the Oracle SYS_CONTEXT() function:

SYS_CONTEXT('namespace', 'parameter' [, length ])

Parameters

'namespace'

Required. The namespace. Oracle provides two built-in namespaces: USERENV (current session) and SYS_SESSION_ROLES (indicates whether specified roles are enabled for the session).

'parameter'

Required. The parameter name.

length

Optional. The maximum return value is 256 bytes, which you can override with this parameter.

Return Value

The Oracle SYS_CONTEXT() function returns the value of the given parameter associated with the given context at the current time.

If any of the parameters are NULL, SYS_CONTEXT() returns NULL.

Oracle SYS_CONTEXT() Examples

Here are some examples that demonstrate how to use the Oracle SYS_CONTEXT() function.

Get the Login Name

The following statement returns the name of the user who logged in to the database:

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

Output:

SYS_CONTEXT('USERENV','SESSION_USER')
________________________________________
SYSTEM

NULL Parameters

If any of the parameters are NULL, SYS_CONTEXT() returns NULL.

SET NULL 'NULL';
SELECT
    SYS_CONTEXT(NULL, NULL) NULL_1,
    SYS_CONTEXT(NULL, NULL, NULL) NULL_2
FROM dual;

Output:

NULL_1    NULL_2
_________ _________
NULL      NULL

In this example, we use the SET NULL 'NULL'; statement to display NULL values as the string 'NULL'.

Conclusion

Oracle SYS_CONTEXT() is a built-in function that returns the value of the given parameter associated with the given context at the current time.

You can find more information about this function here.