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

后端 未结 5 1318
心在旅途
心在旅途 2021-01-01 16:31

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

  • user()
  • sys_context(\'USERENV\', \'CURRENT_USER\')
  • sys_context(\'USEREN
5条回答
  •  情深已故
    2021-01-01 16:47

    There is an important note to take into account when using the USER function from PL/SQL. As I have documented in this blog post, STANDARD.USER() is implemented as follows:

    function USER return varchar2 is
    c varchar2(255);
    begin
        select user into c from sys.dual;
        return c;
    end;
    

    So, it delegates to evaluating user in the SQL engine, which leads to a hidden PL/SQL to SQL context switch. If you're doing that too often, e.g. from within a trigger, then that can turn out to be quite hurtful in a production system. Try to avoid calling USER() from PL/SQL, and use sys_context('USERENV', 'SESSION_USER') instead.

提交回复
热议问题