Purpose
The CREATE CONTEXT statement creates a namespace for a context (a set of application-defined properties used for validating and protecting applications) and associates the namespace with a package created externally to set the context.
Users use the DBMS_SESSION.SET_CONTEXT procedure in the specified package to set or reset the properties of the context.
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
]
Syntax
| Keyword | Description |
|---|---|
| OR REPLACE | Redefines an existing context namespace with a different package. |
| namespace | Specifies the name of the context namespace to create or modify. The context namespace is always stored in the SYS schema. |
| schema | Specifies the schema where the package is located. If you omit the schema, the database uses the current schema. |
| package_name | Specifies the PL/SQL package used to set or reset the context properties in the current session's namespace. |
| ACCESSED GLOBALLY | Indicates that any application context set in the namespace can be accessed throughout the instance. This setting allows multiple sessions to share application properties. |
Examples
Create a context namespace named
dev_context.obclient> CREATE OR REPLACE CONTEXT dev_context USING SYS.DBMS_SESSION ACCESSED GLOBALLY;Use the
SYS_CONTEXTfunction to control data access based on this context. In the following example, theemp_mgmtpackage has defined thedeptnoattribute as the specified department identifier. You can create a view based on thedeptnovalue to restrict access to the base table employee data.-- First, create the emp table. obclient> CREATE TABLE emp ( empno NUMBER(4) PRIMARY KEY, ename VARCHAR2(10), job VARCHAR2(9), mgr NUMBER(4), hiredate DATE, sal NUMBER(7,2), comm NUMBER(7,2), deptno NUMBER(2) ); -- Create a view based on the context. obclient> CREATE VIEW dev_emp_view AS SELECT * FROM emp WHERE deptno = SYS_CONTEXT('dev_context', 'deptno');