CREATE CONTEXT

2023-10-31 11:17:11  Updated

Purpose

You can use the CREATE CONTEXT statement to create a namespace for a context (a group of application-defined attributes that are used to validate and protect the application) and associate the namespace with the package that is externally created to set this context.

You can use the DBMS_SESSION.SET_CONTEXT procedure in the specified package to set or reset the context attributes.

Notice

To create a context namespace, you must have the CREATE ANY CONTEXT system privilege.

Syntax

CREATE [ OR REPLACE ] CONTEXT namespace
USING [ schema. ] package_name
[ INITIALIZED { EXTERNALLY | GLOBALLY }
| ACCESSED GLOBALLY
] ;

Parameters

Parameter Description
OR REPLACE Uses a different package to redefine the existing context namespace.
namespace The name of the context namespace to be created or modified. Context namespaces are always stored in the schema SYS.
schema The schema to which the package belongs. If you do not specify this parameter, the current schema is used.
package_name The name of the PL package that is used to set or reset the context attributes in the namespace of the current session.
ACCESSED GLOBALLY Specifies that any application context set in the namespace can be accessed in the entire instance. This setting allows multiple sessions to share application attributes.

Examples

  • Create a context namespace named dev_context.

    obclient> CREATE OR REPLACE CONTEXT dev_context USING SYS.DBMS_SESSION ACCESSED GLOBALLY;
    Query OK, 0 rows affected
    
  • Use the SYS_CONTEXT() function to control data access based on this context. In the following example, the emp_mgmt package defines the deptno attribute as the identifier of the specified department. You can create a view based on the deptno value to restrict access to employee data in the base table.

    obclient> CREATE VIEW dev_emp_view AS
         SELECT * FROM emp
         WHERE deptno = SYS_CONTEXT('dev_context', 'deptno');
    Query OK, 0 rows affected
    

Contact Us