Oracle SYS_CONTEXT Function with Examples (including Oracle 12C)

The SYS_CONTEXT function is an Oracle function that checks configuration values on the database. Learn how to use it and see some examples in this article.

Purpose of the Oracle SYS_CONTEXT Function

The purpose of the Oracle SYS_CONTEXT function is to return information about the Oracle environment that you're working on.

Syntax

The syntax of the SYS_CONTEXT function is:

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

Parameters

The parameters of the SYS_CONTEXT function are:

  • namespace (mandatory): This is an Oracle namespace that has been created. A common namespace to use for this function is "USERENV". It must be within quotes.
  • parameter (mandatory): A valid parameter attribute that the value is to be returned for. It can be set using the dbms_session.set_context procedure.
  • length (optional): The length of the return value, in bytes. The default value is 256 if this value is not specified.

Some other notes about this function:

  • For the namespace and parameter, you can specify a string or an expression that resolves to a string (e.g. selecting from a table of namespaces)
  • The parameter is not case-sensitive and must be less than or equal to 30 characters.
  • The return type is VARCHAR2.
  • The length value must be a NUMBER or a value that can be converted to a number.
  • The length value can be between 1 and 4000. If the value is invalid, Oracle ignores it and uses the default value (256).
  • The USERENV namespace describes the current session.

Can You List All of the Parameters Used in SYS_CONTEXT?

If by "listing all parameters" you mean "show a list of the parameters I can use in the Oracle SYS_CONTEXT function", then yes. Refer to the table below for the possible inputs into this function.

What Are the Oracle SYS_CONTEXT Parameters for Oracle 12C?

You can find them in the table below, where I've included the parameters for the last 4 major Oracle versions.

Parameters of SYS_CONTEXT for USERENV Namespace

This table lists all of the parameter values that can be used within the USERENV namespace. This can give you a lot of information about the user session.

Parameter Explanation Oracle 9i Oracle 10g Oracle 11g Oracle 12c
ACTION Identifies the position in the module (application name) No Yes Yes Yes
AUDITED_CURSORID Returns the cursor ID of the SQL that triggered the audit Yes Yes Yes Yes
AUTHENTICATED_IDENTITY Returns the identity used in authentication No Yes Yes Yes
AUTHENTICATION_DATA Data being used to authenticate the login user Yes Yes Yes Yes
AUTHENTICATION_METHOD Returns the method of authentication No Yes Yes Yes
AUTHENTICATION_TYPE Describes how the user was authenticated. Can be one of the following values: Database, OS, Network, or Proxy Yes No No No
BG_JOB_ID If the session was established by an Oracle background process, this parameter will return the Job ID. Otherwise, it will return NULL. Yes Yes Yes Yes
CDB_NAME If queried while connected to a multitenant container database (CDB), returns the name of the CDB. Otherwise, returns null. No No No Yes
CLIENT_IDENTIFIER Returns the client identifier, which is used to identify users that authenticate as the same database user Yes Yes Yes Yes
CLIENT_INFO Returns up to 64 bytes of user session information Yes Yes Yes Yes
CLIENT_PROGRAM_NAME The name of the program used for the database session. No No No Yes
CON_ID If queried while connected to a CDB, returns the current container ID. Otherwise, returns 0. No No No Yes
CON_NAME If queried while connected to a CDB, returns the current container name. Otherwise, returns the name of the database as specified in the DB_NAME initialization parameter. No No No Yes
CURRENT_BIND The bind variables for fine-grained auditing No Yes Yes Yes
CURRENT_EDITION_ID The identifier of the current edition. No No No Yes
CURRENT_EDITION_NAME The name of the current edition. No No No Yes
CURRENT_SCHEMA Returns the name of the currently active default schema Yes Yes Yes Yes
CURRENT_SCHEMAID Identifier of the currently active default schema. Yes Yes Yes Yes
CURRENT_SQL Returns the first 4k bytes of the SQL that triggered the audit event Yes Yes Yes Yes
CURRENT_SQLn Returns the n number of 4k bytes of the SQL that triggered the audit event, where n is a number from 1 to 7. For example, CURRENT_SQL1 returns bytes from 4k to 8k, CURRENT_SQL2 returns bytes from 8k to 12k. Yes Yes Yes Yes
CURRENT_SQL_LENGTH Returns the length of the current SQL statement that triggered the audit event No Yes Yes Yes
CURRENT_USER Name of the current user Yes No No No
CURRENT_USERID Userid of the current user Yes No No No
DATABASE_ROLE The database role using the SYS_CONTEXT function with the USERENV namespace. The role is one of the following: PRIMARY, PHYSICAL STANDBY, LOGICAL STANDBY, SNAPSHOT STANDBY. No No No Yes
DB_DOMAIN Domain of the database from the DB_DOMAIN initialization parameter Yes Yes Yes Yes
DB_NAME Name of the database from the DB_NAME initialization parameter Yes Yes Yes Yes
DB_SUPPLEMENTAL_LOG_LEVEL If supplemental logging is enabled, returns a string containing the list of enabled supplemental logging levels. Possible values are: ALL_COLUMN, FOREIGN_KEY, MINIMAL, PRIMARY_KEY, PROCEDURAL, and UNIQUE_INDEX. If supplemental logging is not enabled, returns null. No No No Yes
DB_UNIQUE_NAME Name of the database from the DB_UNIQUE_NAME initialization parameter No Yes Yes Yes
DBLINK_INFO Returns the source of a database link session. Returns a string of "SOURCE_GLOBAL_NAME=dblink_src_global_name, DBLINK_NAME=dblink_name, SOURCE_AUDIT_SESSIONID=dblink_src_audit_sessionid" where: dblink_src_global_name is the unique global name of the source database dblink_name is the name of the database link on the source database dblink_src_audit_sessionid is the audit session ID of the session on the source database that initiated the connection to the remote database using dblink_name No No No Yes
ENTRYID Available auditing entry identifier Yes Yes Yes Yes
ENTERPRISE_IDENTITY Returns the user's enterprise-wide identity No Yes Yes Yes
EXTERNAL_NAME External of the database user Yes No No No
FG_JOB_ID If queried from within a job that was created using the DBMS_JOB package: If the session was established by a client foreground process, this parameter will return the Job ID. Otherwise, it will return NULL. Otherwise, it returns 0 Yes Yes Yes Yes
GLOBAL_CONTEXT_MEMORY The number used in the System Global Area by the globally accessed context Yes Yes Yes Yes
GLOBAL_UID The global user ID from Oracle Internet Directory for enterprise security logins. Returns NULL for all other logins. No No Yes Yes
HOST Name of the host machine from which the client has connected Yes Yes Yes Yes
IDENTIFICATION_TYPE Returns the way the user's schema was created. Specifically, it reflects the IDENTIFIED clause in the CREATE/ALTER USER syntax No Yes Yes Yes
INSTANCE The identifier number of the current instance Yes Yes Yes Yes
INSTANCE_NAME The name of the current instance No Yes Yes Yes
IS_APPLY_SERVER Returns TRUE if queried from within a SQL Apply server in a logical standby database. Otherwise, returns FALSE. No No No Yes
IS_DG_ROLLING_UPGRADE Returns TRUE if a rolling upgrade of the database software in a Data Guard configuration, initiated by way of the DBMS_ROLLING package, is active. Otherwise, returns FALSE. No No No Yes
IP_ADDRESS IP address of the machine from which the client has connected. If the client and server are on the same machine and the connection uses IPv6 addressing, then ::1 is returned. Yes Yes Yes Yes
ISDBA Returns TRUE if the user has DBA privileges. Otherwise, it will return FALSE. Yes Yes Yes Yes
LANG The ISO abbreviation for the language (a shorter form than the existing 'LANGUAGE' parameter) Yes Yes Yes Yes
LANGUAGE The language, territory, and character of the session. In the following format: language_territory.characterset Yes Yes Yes Yes
MODULE Returns the application name set through DBMS_APPLICATION_INFO package or OCI No Yes Yes Yes
NETWORK_PROTOCOL Network protocol being used for communication, as specified in the 'PROTOCOL=protocol' portion of the connect string. Yes Yes Yes Yes
NLS_CALENDAR The calendar of the current session Yes Yes Yes Yes
NLS_CURRENCY The currency of the current session Yes Yes Yes Yes
NLS_DATE_FORMAT The date format for the current session Yes Yes Yes Yes
NLS_DATE_LANGUAGE The language used for dates Yes Yes Yes Yes
NLS_SORT BINARY or the linguistic sort basis Yes Yes Yes Yes
NLS_TERRITORY The territory of the current session Yes Yes Yes Yes
ORACLE_HOME The full path name for the Oracle home directory. No No No Yes
OS_USER The OS username for the user logged in Yes Yes Yes Yes
PLATFORM_SLASH The slash character that is used as the file path delimiter for your platform. No No No Yes
POLICY_INVOKER The invoker of row-level security policy functions No Yes Yes Yes
PROXY_ENTERPRISE_IDENTITY The Oracle Internet Directory DN when the proxy user is an enterprise user No Yes Yes Yes
PROXY_GLOBAL_UID The global user ID from Oracle Internet Directory for enterprise user security proxy users. Returns NULL for all other proxy users. No Yes Yes Yes
PROXY_USER The name of the user who opened the current session on behalf of SESSION_USER Yes Yes Yes Yes
PROXY_USERID The identifier of the user who opened the current session on behalf of SESSION_USER Yes Yes Yes Yes
SCHEDULER_JOB Returns Y if the current session belongs to a foreground job or background job. Otherwise, returns N. No No No Yes
SERVER_HOST The host name of the machine where the instance is running No Yes Yes Yes
SERVICE_NAME The name of the service that the session is connected to No Yes Yes Yes
SESSION_EDITION_ID The identifier of the session edition. No No No Yes
SESSION_EDITION_NAME The name of the session edition. No No No Yes
SESSION_USER The database user name of the user logged in Yes Yes Yes Yes
SESSION_USERID The database identifier of the user logged in Yes Yes Yes Yes
SESSIONID The identifier of the auditing session Yes Yes Yes Yes
SID Session number No Yes Yes Yes
STATEMENTID The auditing statement identifier No Yes Yes Yes
TERMINAL The OS identifier of the current session Yes Yes Yes Yes
UNIFIED_AUDIT_SESSIONID If queried while connected to a database that uses unified auditing or mixed mode auditing, returns the unified audit session ID. If queried while connected to a database that uses traditional auditing, returns null. No No No Yes

What About the Oracle USER Function?

There’s a similar function in Oracle called USER.

The Oracle USER function provides you with the user ID of the current Oracle session.

It’s a simple function that takes no parameters.

The syntax of the USER function is:

1USER

That’s it. No parameters or anything. It can work just like a keyword.

The differences between USER and SYS_CONTEXT are:

  • USER – Returns the name of the session user (the user who logged on). The same as SESSION_USER below.
  • SYS_CONTEXT(‘USERENV’, ‘CURRENT_USER) – The name of the database user whose privileges are currently active. This may change during the session.
  • SYS_CONTEXT(‘USERENV’, ‘SESSION_USER) – The name of the database user at logon. Does not change throughout the session.

What About the Oracle USERENV Function?

The purpose of the USERENV function is to get information about your current session. It’s a simple function but can take a few different values for its parameter.

However, it is a deprecated function.

USERENV is a deprecated function that is retained for compatibility with older versions. Oracle recommends that the SYS_CONTEXT function be used instead.

If you want to stop using USERENV and start using SYS_CONTEXT, you can call the SYS_CONTEXT function, with the first parameter of USERENV and the second parameter equal to the values in the above table.

For example, this function:

1USERENV('CLIENT_INFO')

Can be replaced with:

1SYS_CONTEXT('USER_ENV', 'CLIENT_INFO')

Examples of the Oracle SYS_CONTEXT Function

Here are some examples of the Oracle SYS_CONTEXT function. I find that examples are the best way for me to learn about code, even with the explanation above.

Example 1 - NLS_DATE_LANGUAGE

This example shows what the NLS_DATE_LANGUAGE is on the database.

1SELECT sys_context('USERENV', 'NLS_DATE_LANGUAGE');

Result:

1ENGLISH

Example 2 - NLS_DATE_FORMAT

This example shows what the date format is on the database.

1SELECT sys_context('USERENV', 'NLS_DATE_FORMAT');

Result:

1DD/MON/RR

Example 3 - CURRENT_SCHEMA

This example shows what the current schema is.

1SELECT sys_context('USERENV', 'CURRENT_SCHEMA');

Result:

1PETER

You can find a full list of Oracle SQL functions here.

Lastly, if you enjoy the information and career advice I've been providing, sign up to my newsletter below to stay up-to-date on my articles. You'll also receive a fantastic bonus. Thanks!

comments powered by Disqus