Oracle Database

Page 221

Using Database Session-Based Application Contexts

In this specification: ■

■ ■

namespace: The name of the application context. You can specify either a string or an expression. parameter: A parameter within the namespace application context. length: Optional. The default maximum size of the return type is 256 bytes, but you can override the length by specifying a value up to 4000 bytes. Enter a value that is a NUMBER data type, or a value that can be can be implicitly converted to NUMBER. The data type of the SYS_CONTEXT return type is a VARCHAR2.

The SYS_CONTEXT function provides a default namespace, USERENV, which describes the current session of the user logged on. You can use SYS_CONTEXT to retrieve different types of session-based information about a user, such as the user host computer ID, IP address, operating system user name, and so on. Remember that you only use USERENV to retrieve session data, not set it. The predefined attributes are listed in the description for the SYS_CONTEXT PL/SQL function in the Oracle Database SQL Language Reference. For example, to retrieve the name of the host computer to which a client is connected, you can use the HOST parameter of USERENV as follows: SYS_CONTEXT ('userenv','host')

You can check the SYS_CONTEXT settings by issuing a SELECT SQL statement on the DUAL table. The DUAL table is a small table in the data dictionary that Oracle Database and user-written programs can reference to guarantee a known result. This table has one column called DUMMY and one row that contains the value X. Example 7–2 demonstrates how to find the host computer on which you are logged, assuming that you are logged on to the SHOBEEN_PC host computer under EMP_ USERS. Example 7–2 Finding SYS_CONTEXT Values SELECT SYS_CONTEXT ('userenv', 'host') FROM dual; SYS_CONTEXT(USERENV,HOST) ------------------------EMP_USERS\SHOBEEEN_PC

Note: The USERENV application context namespace replaces the

USERENV function provided in earlier Oracle Database releases.

Using Dynamic SQL with SYS_CONTEXT During a session in which you expect a change in policy between executions of a given query, the query must use dynamic SQL. You must use dynamic SQL because static SQL and dynamic SQL parse statements differently: ■

Static SQL statements are parsed at compile time. They are not parsed again at execution time for performance reasons. Dynamic SQL statements are parsed every time they are executed.

Consider a situation in which Policy A is in force when you compile a SQL statement, and then you switch to Policy B and run the statement. With static SQL, Policy A remains in force. Oracle Database parses the statement at compile time, but does not

Using Application Contexts to Retrieve User Information

7-7


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.