獲得參數定義:
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 ' ——————由前臺進程建立的JOB的ID
'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