博客园  :: 首页  :: 联系 :: 管理

Oracle9i(Version 9.2)SYS_CONTEXT函数的用法

Posted on 2006-10-08 13:50  sunrack  阅读(546)  评论(0)    收藏  举报

这个函数在写一些触发器,函数的时候非常有用处。

用法:SELECT sys_context('USERENV', '<parameter>') FROM dual;

第二个参数的可选值:
AUTHENTICATION_DATA
Data being used to authenticate the login user. For X.503 certificate authenticated sessions, this field returns the context of the certificate in HEX2 format.
Note: You can change the return value of the AUTHENTICATION_DATA attribute using the length parameter of the syntax. Values of up to 4000 are accepted. This is the only attribute of USERENV for which Oracle implements such a change.

AUTHENTICATION_TYPE
How the user was authenticated:
DATABASE: username/password authentication
OS: operating system external user authentication
NETWORK: network protocol or ANO authentication
PROXY: OCI proxy connection authentication

BG_JOB_ID
Job ID of the current session if it was established by an Oracle background process. Null if the session was not established by a background process.

CLIENT_INFO
Returns up to 64 bytes of user session information that can be stored by an application using the DBMS_APPLICATION_INFO package.

CURRENT_SCHEMA
Name of the default schema being used in the current schema. This value can be changed during the session with an ALTER SESSION SET CURRENT_SCHEMA statement.

CURRENT_SCHEMAID
Identifier of the default schema being used in the current session.

CURRENT_USER
The name of the user whose privilege the current session is under.

CURRENT_USERID
User ID of the user whose privilege the current session is under.

DB_DOMAIN
Domain of the database as specified in the DB_DOMAIN initialization parameter.

DB_NAME
Name of the database as specified in the DB_NAME initialization parameter.

ENTRYID
The available auditing entry identifier. You cannot use this option in distributed SQL statements. To use this keyword in USERENV, the initialization parameter AUDIT_TRAIL must be set to true.

EXTERNAL_NAME
External name of the database user. For SSL authenticated sessions using v.503 certificates, this field returns the distinguished name (DN) stored in the user certificate.

FG_JOB_ID
Job ID of the current session if it was established by a client foreground process. Null if the session was not established by a foreground process.

HOST
Name of the host machine from which the client has connected.

INSTANCE
The instance identification number of the current instance.

IP_ADDRESS
IP address of the machine from which the client is connected.

ISDBA
TRUE if you are logged on as SYS.

LANG
The ISO abbreviation for the language name, a shorter form than the existing 'LANGUAGE' parameter.

LANGUAGE
The language and territory currently used by your session, along with the database character set, in the form:language_territory.characterset.

NETWORK_PROTOCOL
Network protocol being used for communication, as specified in the 'PROTOCOL=protocol' portion of the connect string.

NLS_CALENDAR
The current calendar of the current session.

NLS_CURRENCY
The currency of the current session.

NLS_DATE_FORMAT
The date format for the session.

NLS_DATE_LANGUAGE
The language used for expressing dates.

NLS_SORT BINARY
or the linguistic sort basis.

NLS_TERRITORY
The territory of the current session.

OS_USER
Operating system username of the client process that initiated the database session.

PROXY_USER
Name of the database user who opened the current session on behalf of SESSION_USER.

PROXY_USERID
Identifier of the database user who opened the current session on behalf of SESSION_USER.

SESSION_USER
Database user name by which the current user is authenticated. This value remains the same throughout the duration of the session.

SESSION_USERID
Identifier of the database user name by which the current user is authenticated.

SESSIONID
The auditing session identifier. You cannot use this option in distributed SQL statements.

TERMINAL
The operating system identifier for the client of the current session. In distributed SQL statements, this option returns the identifier for your local session. In a distributed environment, this is supported only for remote SELECT statements, not for remote INSERT, UPDATE, or DELETE operations.
(The return length of this parameter may vary by operating system.)

摘自DBASUPPORT.COM
全文连接如下,有第二部分没有摘抄:
http://forums.dbasupport.com/oracle...s_context.shtml

USERENV vs. SYS_CONTEXT
First, let's consider what information the deprecated USERENV function can provide. In this example, I need to determine whether I am logged on as the DBA; what the instance number of the current database is; what terminal I am currently logged on from; and what the NLS Territory parameters have been set to. Here is an example utilizing USERENV to return this information:

SQL> TTITLE CENTER "Example of USERENV() Function"
SQL> COLUMN amidba FORMAT A9 HEADING "Am I DBA?"
SQL> COLUMN instance FORMAT 99999 HEADING "Inst|ID"
SQL> COLUMN terminal FORMAT A16 HEADING "Terminal"
SQL> COLUMN language FORMAT A32 HEADING "NLS Language Parameters"
SQL> SELECT
2 USERENV('ISDBA') amidba
3 ,USERENV('INSTANCE') instance
4 ,USERENV('TERMINAL') terminal
5 ,USERENV('LANGUAGE') language
6 FROM DUAL;
Example of USERENV() Function
Inst
Am I DBA? ID Terminal NLS Language Parameters
--------- ------ ---------------- --------------------------------
FALSE 1 MAIN_CONSOLE AMERICAN_AMERICA.WE8MSWIN1252

And here's its counterpart using SYS_CONTEXT against the USERENV namespace:


SQL> TTITLE CENTER "Example of SYS_CONTEXT() Function"
SQL> COLUMN amidba FORMAT A9 HEADING "Am I DBA?"
SQL> COLUMN instance FORMAT A6 HEADING "Inst|ID"
SQL> COLUMN terminal FORMAT A16 HEADING "Terminal"
SQL> COLUMN language FORMAT A32 HEADING "NLS Language Parameters"
SQL>
SQL> SELECT
2 SYS_CONTEXT('USERENV', 'ISDBA') amidba
3 ,SYS_CONTEXT('USERENV', 'INSTANCE') instance
4 ,SYS_CONTEXT('USERENV', 'TERMINAL') terminal
5 ,SYS_CONTEXT('USERENV', 'LANGUAGE') language
6 FROM DUAL;

Example of SYS_CONTEXT() Function
Inst
Am I DBA? ID Terminal NLS Language Parameters
--------- ------ ---------------- --------------------------------
FALSE 1 MAIN_CONSOLE AMERICAN_AMERICA.WE8MSWIN1252

Expanded Session Information
SYS_CONTEXT is more than just a replacement for USERENV, as the next example shows. There are over two dozen namespace attributes that can be queried. This next example shows how to return:

the session's IP address
the session's host machine name
the network protocol in use
the current user and schema
whether the session is a foreground task
Here is a sample of some session and user-specific information that can be obtained in one function call:


SQL> TTITLE CENTER "More Session-Level Information from SYS_CONTEXT()"
SQL> COLUMN ipaddr FORMAT A15 HEADING "IP Address"
SQL> COLUMN host FORMAT A20 HEADING "Host"
SQL> COLUMN netprtc FORMAT A8 HEADING "Network|Protocol"
SQL> COLUMN curruser FORMAT A8 HEADING "Current|User"
SQL> COLUMN currschema FORMAT A8 HEADING "Current|Schema"
SQL> COLUMN fgjob FORMAT A4 HEADING "FG|Job?"
SQL> COLUMN bgjob FORMAT A4 HEADING "BG|Job?"
SQL>
SQL> SELECT
2 SYS_CONTEXT('USERENV', 'IP_ADDRESS', 15) ipaddr
3 ,SYS_CONTEXT('USERENV', 'HOST', 16) host
4 ,SYS_CONTEXT('USERENV', 'NETWORK_PROTOCOL', 8) netprtc
5 ,SYS_CONTEXT('USERENV', 'CURRENT_USER', 8) curruser
6 ,SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA', 8) currschema
7 ,SYS_CONTEXT('USERENV', 'FG_JOB_ID', 4) fgjob
8 ,SYS_CONTEXT('USERENV', 'BG_JOB_ID', 4) bgjob
9 FROM DUAL;

More Session-Level Information from SYS_CONTEXT()
Network Current Current FG BG
IP Address Host Protocol User Schema Job? Job?
--------------- -------------------- -------- -------- -------- ---- ----
198.63.66.124 WORKGROUP\MYCONSOLE tcp HR HR 0

With the exception of one parameter for the USERENV namespace - AUTHENTICATION_DATA, which will return the data being used to authenticate the login user - the maximum length of the VARCHAR2 string that SYS_CONTEXT returns is 255 characters, depending upon the parameter chosen. However, note from the previous examples that the SYS_CONTEXT function can also take one other argument after the specified parameter to limit the maximum length of the returned value.

oracle官方文档的链接:
USERENV
http://www.lc.leidenuniv.nl/awcours...nctions162a.htm

SYS_CONTEXT
http://www.lc.leidenuniv.nl/awcours...22a.htm#1038178