sys_context与userenv函数

获得参数定义:

SQL> desc sys.standard.sys_context

Parameter   Type     Mode Default?

----------- -------- ---- --------

(RESULT)    VARCHAR2              

NAMESPACE   VARCHAR2 IN           

ATTRIBUTE   VARCHAR2 IN           

(RESULT)    VARCHAR2               

NAMESPACE   VARCHAR2 IN           

ATTRIBUTE   VARCHAR2 IN           

NEWOPTIONAL VARCHAR2 IN           

 

SQL> desc sys.standard.USERENV

Parameter Type     Mode Default?

--------- -------- ---- --------

(RESULT)  VARCHAR2              

ENVSTR    VARCHAR2 IN 

 

对于USERENV函数是获取用户环境的参数

Oracle对于userenv的定义:

-- Special: if the ICD raises ICD_UNABLE_TO_COMPUTE, that means we should do

  -- the old 'select userenv(...) from dual;' thing.  This allows us to do the

  -- select from PL/SQL rather than having to do it from C (within the ICD.)

  function USERENV (envstr varchar2) return varchar2 is

  c varchar2(255);

  begin

    c := upper(envstr);

 

    -- Gaak: we can't replace the following with a single block of code based

    -- around 'USERENV(c)' because passing USERENV() anything but a string

    -- literal parameter result in ORA-2003: Invalid USERENV parameter!  This

    -- also means that we must manually update this file whenever RDBMS adds a

    -- new option.

    if c = 'COMMITSCN' then

      raise USERENV_COMMITSCN_ERROR;

    elsif c = 'TERMINAL' then

      begin

        c := pesuen(c);

      exception

        when ICD_UNABLE_TO_COMPUTE then

          select userenv('TERMINAL') into c from sys.dual;

      end;

    elsif c = 'ENTRYID' then

      begin

        c := pesuen(c);

      exception

        when ICD_UNABLE_TO_COMPUTE then

          select userenv('ENTRYID') into c from sys.dual;

      end;

    elsif c = 'SESSIONID' then

      begin

        c := pesuen(c);

      exception

        when ICD_UNABLE_TO_COMPUTE then

          select userenv('SESSIONID') into c from sys.dual;

      end;

    elsif c = 'LANGUAGE' then

      begin

        c := pesuen(c);

      exception

        when ICD_UNABLE_TO_COMPUTE then

          select userenv('LANGUAGE') into c from sys.dual;

      end;

    elsif c = 'LANG' then

      begin

        c := pesuen(c);

      exception

        when ICD_UNABLE_TO_COMPUTE then

          select userenv('LANG') into c from sys.dual;

      end;

    elsif c = 'INSTANCE' then

      begin

        c := pesuen(c);

      exception

        when ICD_UNABLE_TO_COMPUTE then

          select userenv('INSTANCE') into c from sys.dual;

      end;

    elsif c = 'CLIENT_INFO' then

      begin

        c := pesuen(c);

      exception

        when ICD_UNABLE_TO_COMPUTE then

          select userenv('CLIENT_INFO') into c from sys.dual;

      end;

    elsif c = 'ISDBA' then

      begin

        c := pesuen(c);

      exception

        when ICD_UNABLE_TO_COMPUTE then

          select userenv('ISDBA') into c from sys.dual;

      end;

    elsif c = 'SCHEMAID' then

      begin

        c := pesuen(c);

      exception

        when ICD_UNABLE_TO_COMPUTE then

          select userenv('SCHEMAID') into c from sys.dual;

      end;

    elsif c = 'SID' then

      begin

        c := pesuen(c);

      exception

        when ICD_UNABLE_TO_COMPUTE then

          select userenv('SID') into c from sys.dual;

      end;

    elsif c = 'PID' then

      begin

        c := pesuen(c);

      exception

        when ICD_UNABLE_TO_COMPUTE then

          select userenv('PID') into c from sys.dual;

      end;

    else

      raise INVALID_USERENV_PARAMETER;

    end if;

    return c;

  end;

也就是说有以下参数可选:

'TERMINAL'     ——————终端主机名

'ENTRYID'     ——————可用来审计条目的标识符

'SESSIONID'   ——————会话ID

'LANGUAGE'    ——————客户端语言

'LANG'        ——————对语言的缩写

'INSTANCE'    ——————当前实例的标识号

'CLIENT_INFO' ——————客户端信息(客户机、服务器在同一机器上得到null

'ISDBA'       —————是否DBA

'SCHEMAID'    ——————SCHEMA ID

'SID'         ——————

'PID'         ——————

 

 

 

 

 

对于SYS_CONTEXT是获取

Oracle对于Sys_context的定义:

-- Special: if the ICD raises ICD_UNABLE_TO_COMPUTE, that means we should do

  -- the old 'select sys_context(...) from dual;' thing.  This allows us to do

  -- the select from PL/SQL rather than having to do it from C (within the ICD.)

  function SYS_CONTEXT(namespace varchar2, attribute varchar2)

    return varchar2 is

  c varchar2(4000);

  BEGIN

    c := pessysctx2(namespace, attribute);

    return c;

  exception

    when ICD_UNABLE_TO_COMPUTE then

      select sys_context(namespace,attribute) into c from sys.dual;

      return c;

  end;

 

namespace=’’,有以下参数可选:

'AUDITED_CURSORID'         ——————触发审计的SQL的游标号

'AUTHENTICATION_DATA '   ——————用来认证的数据

'AUTHENTICATION_TYPE '   ——————认证类型

'BG_JOB_ID '             ——————后台进程启动的当前会话的JOBID

'CLIENT_IDENTIFIER '     ——————客户端Session标识符

'CLIENT_INFO '            ——————认证类型客户端信息

'CURRENT_SCHEMA'          ——————当前SCHEMA

'CURRENT_SCHEMAID '        ——————当前SCHEMA的ID

'CURRENT_SQL '            ——————当前触发精细审计事件的SQL

'CURRENT_USER '           ——————当前用户

'CURRENT_USERID'          ——————当前用户的ID

'DB_DOMAIN '              ——————当前数据库的域

'DB_NAME '               ——————当前数据库名称

'ENTRYID '               ——————可用来审计条目的标识符

'EXTERNAL_NAME '          ——————当前用户的外部名称

'FG_JOB_ID '              ——————由前台进程建立的JOBID

'GLOBAL_CONTEXT_MEMORY'  ——————正被使用的系统全局区

'HOST'                    ——————当前主机名

'INSTANCE '              ——————当前实例的标识号

'IP_ADDRESS '             ——————客户端IP地址

'ISDBA '                 ——————是否DBA

'LANG '                  ——————当前session的语言的缩写

'LANGUAGE '               ——————当前session的语言

'NETWORK_PROTOCOL '        ——————当前使用的网络协议

'NLS_CALENDAR '            ——————当前会话的日历类型

'NLS_CURRENCY'             ——————当前会话的货币类型

'NLS_DATE_FORMAT'         ——————当前会话的日期格式

'NLS_DATE_LANGUAGE'        ——————当前会话的语言类型

'NLS_SORT '               ——————当前的排序方式

'NLS_TERRITORY '          ——————当前会话的国家

'OS_USER '                ——————建立会话的操作系统用户

'PROXY_USER '             ——————依靠session_user建立当前会话的用户

'PROXY_USERID '            ——————依靠session_user建立当前会话的用户的ID

'SESSION_USER '            ——————当前会话的用户

'SESSION_USERID '         ——————当前会话的用户的ID

'SESSIONID '              ——————当前会话的标识符

'TERMINAL '               ——————终端名称

 

 

二者之间的差别:

1、  sys_context提供更多的可以选取的值;

 

2、  sys_context函数可以从自己设置的context中选取值:

一个例子:

第一步,创建context:

create or replace context zianed

using hello

accessed globally;

第二步,创建包

create or replace package hello

is

       procedure init;

end hello;

 

create or replace package body hello is

 

  -- Private type declarations

 

  procedure init as

  begin

    DBMS_SESSION.SET_CONTEXT(NAMESPACE => 'ZIANED',

                             ATTRIBUTE => 'hello',

                             VALUE     => 'Hello ' || user);

  end init;

end hello;

第三步,测试程序:

-- Created on 2008-12-14 by ADMINISTRATOR

declare

  -- Local variables here

  v_out varchar2(100) default '';

begin

  hello.init;

  select sys_context('zianed', 'hello') into v_out from dual;

  dbms_output.put_line('zianed.hello='||v_out);

end;

最终得到输出zianed.hello=Hello SYSTEM

3、   

 

 

 

 

 

 

 

 

 

 

参考文献:

SQL Reference

Expert one on one Oracle

dbasupport

http://forums.dbasupport.com/oracle/ora9i/sys_context.shtml

http://forums.dbasupport.com/oracle/ora9i/sys_context2.shtml

 

 


oracle官方文档的链接:
USERENV
http://www.lc.leidenuniv.nl/awco ... 0/functions162a.htm
SYS_CONTEXT
http://www.lc.leidenuniv.nl/awco ... ons122a.htm#1038178

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值