Purpose
The CREATE CONTEXT statement can create a namespace for a context (a set of application-defined attributes used for authentication and securing applications). It can also associate the namespace with an externally created package used to set that context.
Users use the DBMS_SESSION.SET_CONTEXT procedure in the specified package to set or reset the attributes 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 description
| Parameter | Description |
|---|---|
| OR REPLACE | Redefines an existing context namespace with a different package. |
| namespace | The name of the context namespace to create or modify. Context namespaces are always stored in the SYS schema. |
| schema | The schema in which the package is stored. If you do not specify the schema, the current database schema is used. |
| package_name | The PL package in the current user session that is used to set or reset the attributes of the context. |
| ACCESSED GLOBALLY | Indicates that any context set in the namespace can be accessed throughout the 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;Use the
SYS_CONTEXTfunction to control data access based on this context. In the following example, theemp_mgmtpackage has defined thedeptnoattribute as the department identifier. You can create a view based on thedeptnovalue to restrict access to employee data in the base table.-- 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');