Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • oracle Dba site Oracle
  • Removing first line Linux/Unix
  • Shuffle an array PHP/MYSQL/Wordpress
  • How To Transfer Passwords Between Databases (ref note: 199582.1) Oracle
  • crtgr.sql /* For creating trigger from data dictionary */ Oracle
  • Processes parameter and its dependencies on OS kernel parameters Linux/Unix
  • Search and replace pattern Linux/Unix
  • Rman Notes -1 Oracle
  • v$backup.status information Oracle
  • oradebug ipcrm ipcs Oracle
  • V$transaction notes for finding XID composition. Oracle
  • SQL Server: How to see current transactions or requests SQL Server
  • Adding or Dropping Online Redo Log Files When Physical Standby in place Oracle
  • sid_wise_cursor.sql find open cursor basis on username or SID Oracle
  • Zip and unzip with tar Linux/Unix

Building Our Own Namespaces with “Create Context”

Posted on 11-Oct-2005 By Admin No Comments on Building Our Own Namespaces with “Create Context”

**************************************

**** Building Our Own Namespaces *****

**************************************

The USERENV namespace does store extensive information, but the power of SYS_CONTEXT does not stop there. You can also create secured namespaces and store context in them for retrieval within a session or across the instance.

For example, if I create a new namespace via the CREATE CONTEXT command, I can then use SYS_CONTEXT to manage and control access to that namespace. In the example below (executed from the SYSTEM login), I’ve made the namespace accessible to any session for the database instance by specifying ACCESSED GLOBALLY

SQL> CREATE OR REPLACE CONTEXT hr_security

2 USING hr.pkg_security

3 ACCESSED GLOBALLY;

Context created.

Next, I create the corresponding package that will allow me to set parameters in the newly created context via calls to the DBMS_SESSION.SET_CONTEXT procedure:

SQL> CREATE OR REPLACE PACKAGE hr.pkg_security

2 IS

3

4 PROCEDURE set_security(

5 a_vcParameter VARCHAR2,

6 a_vcValue VARCHAR2

7 );

8

9 FUNCTION empname

10 RETURN VARCHAR2;

11

12 END pkg_security;

13 /

Package created.

SQL> CREATE OR REPLACE PACKAGE BODY hr.pkg_security

2 IS

3

4 PROCEDURE set_security(

5 a_vcParameter VARCHAR2,

6 a_vcValue VARCHAR2

7 )

8 IS

9 — Sets value for specified parameter in HRSECURITY namespace

10 BEGIN

11 DBMS_SESSION.SET_CONTEXT(

12 NAMESPACE => ‘HR_SECURITY’

13 ,ATTRIBUTE => a_vcParameter

14 ,VALUE => a_vcValue

15 );

16

17 END set_security;

18

19 FUNCTION empname

20 RETURN VARCHAR2

21 IS

22 — Returns employee’s name using employee ID set via SET_SECURITY parameter

23 vcEmpName VARCHAR2(64) := NULL;

24 BEGIN

25 SELECT last_name || ‘, ‘ || first_name

26 INTO vcEmpName

27 FROM hr.employees

28 WHERE employee_id = TO_NUMBER(SYS_CONTEXT(‘HR_SECURITY’, ‘EMPLOYEE_ID’));

29 RETURN vcEmpName;

30 END empname;

31

32 END pkg_security;

33 /

Package body created.

In this package, I have specified a call to the DBMS_SESSION.SET_CONTEXT procedure to create a new parameter and populate a corresponding value in the HR_SECURITY namespace. I’ve also built a function that returns a formatted string containing the employee’s last and first names based on the value stored in that namespace for EMPLOYEE_ID in the HR_SECURITY namespace.

The script below shows the results of calling the new package to set the value for the EMPLOYEE_ID parameter within the namespace and then using SYS_CONTEXT to retrieve the value from the namespace to get the employee’s name:

SQL> SET SERVEROUTPUT ON

SQL> DECLARE

2 lvc_employee_id VARCHAR2(255) := NULL;

3 lvc_empname VARCHAR2(64) := NULL;

4

5 BEGIN

6 PKG_SECURITY.set_security(’employee_id’, ‘302’);

7

8 BEGIN

9 SELECT SYS_CONTEXT(‘HR_SECURITY’, ‘EMPLOYEE_ID’)

10 INTO lvc_employee_id

11 FROM DUAL;

12 END;

13 DBMS_OUTPUT.PUT_LINE( ‘Value for HR_SECURITY.EMPLOYEE_ID via USERENV call is ‘ || lvc_employee_id);

14

15 lvc_empname:= PKG_SECURITY.EMPNAME;

16 DBMS_OUTPUT.PUT_LINE(‘Employee Name: ‘ || lvc_empname);

17

18 END;

19 /

Value for HR_SECURITY.EMPLOYEE_ID via USERENV call is 100

Employee Name: King, Steven

PL/SQL procedure successfully completed.

Conclusion

I have not completely explored the myriad ways SYS_CONTEXT can make my life as a DBA and PL/SQL developer easier, but it holds a lot of promise for securing sensitive information when using other namespaces besides USERENV. I’m hoping that this versatile function will be expanded to utilize other

Oracle-populated namespaces in future releases of Oracle.

Oracle, SQL scripts

Post navigation

Previous Post: Roles and Stored Object behaviour
Next Post: Updated LCK.SQL file.

Related Posts

  • crtgr.sql /* For creating trigger from data dictionary */ Oracle
  • Find Plan Hash value fphv.sql Oracle
  • _B_TREE_BITMAP_PLANS issue during 8.1.7 to 9.2.0.8 upgrade Oracle
  • column level grant syntax Oracle
  • Locally Managed Tablespace and Dictionary managed tablespace (LMT-DMT) Oracle
  • Roles and Stored Object behaviour Oracle

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Ansible (0)
  • AWS (2)
  • Azure (1)
  • Django (0)
  • GIT (1)
  • Linux/Unix (149)
  • MYSQL (5)
  • Oracle (394)
  • PHP/MYSQL/Wordpress (10)
  • POSTGRESQL (1)
  • Power-BI (0)
  • Python/PySpark (7)
  • RAC (17)
  • rman-dataguard (26)
  • shell (149)
  • SQL scripts (343)
  • SQL Server (6)
  • Uncategorized (0)
  • Videos (0)

Recent Posts

  • tracksqltime.sql05-Mar-2026
  • Complete Git Tutorial for Beginners25-Dec-2025
  • Postgres DB user and OS user.25-Dec-2025
  • Trace a SQL session from another session using ORADEBUG30-Sep-2025
  • SQL Server Vs Oracle Architecture difference25-Jul-2025
  • SQL Server: How to see historical transactions25-Jul-2025
  • SQL Server: How to see current transactions or requests25-Jul-2025
  • T-SQL Vs PL/SQL Syntax25-Jul-2025
  • Check SQL Server edition25-Jul-2025
  • Checking SQL Server Version25-Jul-2025

Archives

  • 2026
  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • create PLAN_TABLE command. Oracle
  • Insert cause enqueue locks Oracle
  • SQL_PLAN.sql for checking real execution plan Oracle
  • Search and Replace vi editor command. Linux/Unix
  • Jai Shree Ram Oracle
  • Oracle 10g Wait Model Oracle
  • Monitor and Trace Unix processes using truss Linux/Unix
  • SAN Linux/Unix

Copyright © 2026 pvmehta.com.

Powered by PressBook News WordPress theme