Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between USER() and SYS_CONTEXT('USERENV','CURRENT_USER')?

Tags:

In an Oracle Database, what are the differences between the following:

  • user()
  • sys_context('USERENV', 'CURRENT_USER')
  • sys_context('USERENV', 'SESSION_USER')

Are these also possible related values to whatever 'the current user' is?

  • sys_context('USERENV', 'CURRENT_SCHEMA')
  • sys_context('USERENV', 'AUTHENTICATED_IDENTITY')

I am specifically interested in which ones can change, what can change them, which ones can not change value, which ones have different values based on connection type, and which one(s) is(are) always the schema used to log into the database.

In most of my testing the values are always the same. The only exception would be when running the following to alter 'CURRENT_SCHEMA':

alter session set current_schema=<SCHEMA>

Doing following results in an error:

alter session set current_user=<USER> --even as sys/system, which is good I suppose

So there is some kind of security/rules around all of this. However there must be some reason behind having a SESSION_USER and a CURRENT_USER. I also suppose user() could be a shortcut to sys_context('USERENV', 'CURRENT_USER'), but I could find no documentation on the matter.

like image 781
Andrew Martinez Avatar asked Jun 12 '12 18:06

Andrew Martinez


People also ask

What is userenv (' lang ') in Oracle?

USERENV is a legacy function that is retained for backward compatibility. Oracle recommends that you use the SYS_CONTEXT function with the built-in USERENV namespace for current functionality. See SYS_CONTEXT for more information. USERENV returns information about the current session.

What is the use of SYS_CONTEXT function in Oracle?

SYS_CONTEXT returns the value of parameter associated with the context namespace . You can use this function in both SQL and PL/SQL statements. For namespace and parameter , you can specify either a string or an expression that resolves to a string designating a namespace or an attribute.


1 Answers

From the manual at: http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions184.htm#SQLRF51825

CURRENT_USER

The name of the database user whose privileges are currently active. This may change during the duration of a session to reflect the owner of any active definer's rights object. When no definer's rights object is active, CURRENT_USER returns the same value as SESSION_USER. When used directly in the body of a view definition, this returns the user that is executing the cursor that is using the view; it does not respect views used in the cursor as being definer's rights.

SESSION_USER

The name of the database user at logon. For enterprise users, returns the schema. For other users, returns the database user name. This value remains the same throughout the duration of the session.

So there is a difference between SESSION_USER and CURRENT_USER especially when CURRENT_USER is used in a stored procedure or function.

I have to admit that I don't know what the term "enterprise user" means though.

Btw: there is a third one:

SESSION_USERID

The identifier of the database user at logon.

like image 139
a_horse_with_no_name Avatar answered Sep 20 '22 18:09

a_horse_with_no_name