Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Table of Contents
1 - About
2 - Articles Related
3 - Privileges
4 - Steps
4.1 - Creation of the Contexts
4.2 - Creation of the package
4.3 - Call in the Context empno_ctx
4.4 - Call in the Context myContext
5 - Documentation / Reference
1 - About
An application context is a set of (name-value|variable-value) pairs that Oracle Database stores in
memory during asession.
Oracle provides a built-in namespace calledUSERENV, which describes the current session
parameter.
2 - Articles Related
Oracle Database - Session
Oracle Database - Userenv (Current Session Informations)
3 - Privileges
As Sysdba:
-- SYSTEM PRIVILEGES
GRANT CREATE ANY CONTEXT TO "HR" ;
4 - Steps
A context must be use within a package otherwise you will get an ORA-01031: insufficient
privileges.
CREATE OR REPLACE
PACKAGE set_context_variable_package
IS
PROCEDURE set_empno;
PROCEDURE set_variable_value(
n VARCHAR2,
v VARCHAR2);
END;
/
CREATE OR REPLACE
PACKAGE BODY set_context_variable_package
IS
-- dbms_session.set_context
-- can only be called within the package to which it belongs
-- If you try to execute DBMS_SESSION.SET_CONTEXT you'll get an error, as
-- shown here:
-- ORA-01031: insufficient privileges
PROCEDURE set_empno
IS
emp_id NUMBER;
BEGIN
SELECT
EMPLOYEE_ID
INTO
emp_id
FROM
HR.EMPLOYEES
WHERE
email = SYS_CONTEXT('USERENV', 'SESSION_USER');
EXCEPTION
END;
PROCEDURE set_variable_value(
n VARCHAR2,
v VARCHAR2)
AS
https://gerardnico.com/wiki/db/oracle/context 2/3
16/12/2017 Oracle Database - (Application) Context [Gerardnico]
AS
BEGIN
DBMS_SESSION.set_context('myContext',n,v);
END;
END;
/
https://gerardnico.com/wiki/db/oracle/context 3/3