Purpose
This function returns the value of the specified parameter associated with the current context namespace.
Syntax
SYS_CONTEXT('namespace', 'parameter' [, length ])
Parameters
| Parameter | Description |
|---|---|
| namespace | Specifies the namespace. It can be any USERENV namespace. |
| parameter | Specifies the parameter associated with the namespace. The following table lists the predefined parameters for the USERENV namespace. |
| length | Specifies the length of the return value in bytes. It is an optional parameter. The value ranges from [1, 4000], with a default value of 256 bytes. |
Predefined parameters for the USERENV namespace
| Parameter Name | Return Value |
|---|---|
| CON_ID | Returns the current container ID if the query is executed while connected to a CDB (Container Database). Otherwise, it returns 0. |
| CON_NAME | Returns the current container name if the query is executed while connected to a CDB. Otherwise, it returns the database name specified by the DB_NAME parameter. |
| CURRENT_SCHEMA | The name of the currently active default schema. |
| CURRENT_SCHEMAID | The ID of the currently active default schema. |
| CURRENT_USER | The username of the database user with active privileges. |
| CURRENT_USERID | The ID of the database user with active privileges. |
| DB_NAME | The name of the database. |
| INSTANCE | The identifier of the current instance. |
| INSTANCE_NAME | The name of the current instance. |
| IP_ADDRESS | The IP address of the client machine. |
| LANG | The abbreviation of the language, which is shorter than the format specified by the LANGUAGE parameter. |
| LANGUAGE | The language and region used in the current session, along with the database character set. |
| SESSION_USER | The username of the database user who logged in, which remains unchanged throughout the session. |
| SESSION_USERID | The ID of the database user who logged in. |
| SID | SESSIONID | The Client Session ID of the current session, which uniquely identifies the session on the client. |
Return type
VARCHAR2
Examples
Query the Client Session ID of the session in the USERENV namespace.
obclient [SYS]> SELECT SYS_CONTEXT ('USERENV', 'SESSIONID') AS Client_Session_ID FROM DUAL;
or
obclient [SYS]> SELECT SYS_CONTEXT ('USERENV', 'SID') AS Client_Session_ID FROM DUAL;
The return result is as follows:
+-------------------+
| CLIENT_SESSION_ID |
+-------------------+
| 3221488043 |
+-------------------+
1 row in set
References
You can also use the SHOW PROCESSLIST statement to query the number of sessions and their IDs in the current database. For more information, see View tenant sessions.
