Skip to Main Content
  • Questions
  • Can you Override USER built-in function ?

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Vikram.

Asked: June 07, 2018 - 3:42 pm UTC

Last updated: June 11, 2018 - 6:56 am UTC

Version: 3.2.20.10

Viewed 1000+ times

You Asked

Hi Tom,

Good Morning.
My question may be naive, I'm not an Oracle expert.

Can the USER built-in function be overridden ?

Context:
My current team has used Oracle for decades ( along with power-builder). We are now transitioning to a new web architecture system wherein a single Oracle user ID will be used to to connect to the Oracle DB, making the USER function unusable.
We have used the USER function across hundreds of stored procedures and will now have to replace the built-in USER function with code to pass a parameter to each procedure and replace USER with the newly passed parameter. ( can we avoid this)

Can you kindly suggest a better approach. Can we globally set a value for USER and let the stored procedures continue using USER function.

[sorry, no examples :) ]

regards,
Vikram


and Connor said...

No it cannot be overridden, but you don't need to. Let's jump down to:

can we avoid this

Definitely. And ideally what you want to be doing is using proxy users. See here for details

https://asktom.oracle.com/pls/apex/asktom.search?tag=proxy-users

In this way, when (say) "JOHN" connects, he will be using a session that is connected by "APP_USER" (the common user you were referring to in your question), and you will have access to *both* values (JOHN and APP_USER) for the purpose of auditing etc.

But even without that, the "quick and dirty" workaround is to use a context variable. When people connect to the application, have them do something like:

dbms_session.set_identifier('JOHN');

and then just do a simple find-replace of occurrences in your code of "USER" and replace with "sys_context('USERENV','CLIENT_IDENTIFIER')"

eg

SQL> exec dbms_session.set_identifier('JOHN');

PL/SQL procedure successfully completed.

SQL> select sys_context('USERENV','CLIENT_IDENTIFIER') from dual;

SYS_CONTEXT('USERENV','CLIENT_IDENTIFIER')
-------------------------------------------------
JOHN




Is this answer out of date? If it is, please let us know via a Comment

More to Explore

APEX

Keep your APEX skills fresh by attending their regular Office Hours sessions.