`
tjmzgn
  • 浏览: 159720 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Decode函数的语法

阅读更多
Decode函数的语法结构如下:

decode (expression, search_1, result_1)
decode (expression, search_1, result_1, search_2, result_2)
decode (expression, search_1, result_1, search_2, result_2, ...., search_n, result_n)

decode (expression, search_1, result_1, default)
decode (expression, search_1, result_1, search_2, result_2, default)
decode (expression, search_1, result_1, search_2, result_2, ...., search_n, result_n, default)



decode函数比较表达式和搜索字,如果匹配,返回结果;如果不匹配,返回default值;如果未定义default值,则返回空值。
以下是一个简单测试,用于说明Decode函数的用法:

SQL> create table t as select username,default_tablespace,lock_date from dba_users;Table created.SQL> select * from t;USERNAME                       DEFAULT_TABLESPACE             LOCK_DATE------------------------------ ------------------------------ ---------SYS                            SYSTEMSYSTEM                         SYSTEMOUTLN                          SYSTEMCSMIG                          SYSTEMSCOTT                          SYSTEMEYGLE                          USERSDBSNMP                         SYSTEMWMSYS                          SYSTEM                         20-OCT-048 rows selected.SQL> select username,decode(lock_date,null,'unlocked','locked') status from t;USERNAME                       STATUS------------------------------ --------SYS                            unlockedSYSTEM                         unlockedOUTLN                          unlockedCSMIG                          unlockedSCOTT                          unlockedEYGLE                          unlockedDBSNMP                         unlockedWMSYS                          locked8 rows selected.SQL> select username,decode(lock_date,null,'unlocked') status from t;USERNAME                       STATUS------------------------------ --------SYS                            unlockedSYSTEM                         unlockedOUTLN                          unlockedCSMIG                          unlockedSCOTT                          unlockedEYGLE                          unlockedDBSNMP                         unlockedWMSYS8 rows selected. ------------------------------------------------------------------------------------------------------------------------------------------------

userenv命令的语法:

USERENV returns information about the current session. This information can be useful for writing an application-specific audit trail table or for determining the language-specific characters currently used by your session. You cannot use USERENV in the condition of a CHECK constraint. Table 6-3 describes the values for the parameter argument.

All calls to USERENV return VARCHAR2 data except for calls with the SESSIONID, ENTRYID, and COMMITSCN parameters, which return NUMBER.

Table 6-3 Parameters of the USERENV Function
Parameter Return Value
CLIENT_INFO
CLIENT_INFO returns up to 64 bytes of user session information that can be stored by an application using the DBMS_APPLICATION_INFO package.

Caution: Some commercial applications may be using this context value. Check the applicable documentation for those applications to determine what restrictions they may impose on use of this context area.

See Also:

Oracle9i Database Concepts for more on application context
CREATE CONTEXT and SYS_CONTEXT

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

ISDBA
ISDBA returns 'TRUE' if the user has been authenticated as having DBA privileges either through the operating system or through a password file.

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

LANGUAGE
LANGUAGE returns the language and territory currently used by your session along with the database character set in this form:

language_territory.characterset

SESSIONID
SESSIONID returns your auditing session identifier. You cannot use this attribute in distributed SQL statements.

TERMINAL
TERMINAL returns the operating system identifier for your current session's terminal. In distributed SQL statements, this attribute 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.



Examples
The following example returns the LANGUAGE parameter of the current session:

SELECT USERENV('LANGUAGE') "Language" FROM DUAL;

Language
-----------------------------------
AMERICAN_AMERICA.WE8DEC
----------------------------------------------------------------------------------------------------------------------------------------------------

sys_contex()函数作用:

Oracle9i(Version 9.2)SYS_CONTEXT函数的用法

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

用法: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.)

分享到:
评论

相关推荐

    decode函数.docx

    它的语法结构为 `decode(条件,值 1,返回值 1,值 2,返回值 2,...值 n,返回值 n,缺省值)`。decode 函数的含义是,如果条件等于值 1,则返回返回值 1,如果条件等于值 2,则返回返回值 2,以此类推,如果条件不等于任何...

    关于decode函数的使用

    ### 关于decode函数的使用详解 #### 函数概述 `decode()`函数在SQL语言中是一种常用的条件函数,它可以根据指定的表达式返回不同的值。这种功能对于数据转换、分类统计等场景非常有用。 #### 主要作用 `decode()`...

    Oracle DECODE函数语法使用介绍

    Oracle DECODE函数功能很强,下面就为您详细介绍Oracle DECODE函数的用法,希望可以让您对Oracle DECODE函数有更多的了解。 Oracle DECODE函数 Oracle DECODE函数是Oracle公司独家提供的功能,它是一个功能很强的...

    关于oracle decode函数的用法

    DECODE函数的基本语法如下: ``` DECODE(expression, search_1, result_1, search_2, result_2, ..., default) ``` 其中,`expression`是要检查的值,`search_n`是与`expression`进行比较的条件,而`result_n`是当...

    decode函数借鉴.pdf

    DECODE 函数的语法是:DECODE(value, if1, then1, if2, then2, if3, then3, ..., else),表示如果 value 等于 if1 时,DECODE 函数的结果返回 then1,...,如果不等于任何一个 if 值,则返回 else。初看起来,DECODE...

    decode函数.pdf

    Decode函数是Oracle数据库中用于实现条件逻辑的一个内置函数,它类似于编程语言中的switch-case语句或if-then-else语句。Decode函数可以对给定的表达式进行多个条件判断,并返回匹配条件的相应值。Decode函数广泛...

    decode函数[归类].pdf

    DECODE函数的基本语法如下: `DECODE(value, if1, then1, if2, then2, ..., else)` - `value`: 这是被比较的值,可以是表中的任何列或者计算结果。 - `if1, if2, ...`: 这些是测试条件,用来与`value`进行比较。 -...

    Oracle 中 decode 函数用法

    DECODE函数的基本语法如下: ```sql DECODE(条件, 值1, 返回值1, 值2, 返回值2, ..., 值n, 返回值n, 缺省值) ``` 这个函数的工作原理是检查条件是否等于给定的一系列值。如果条件等于第一个值,则返回相应的第二个值...

    oracle的decode函数

    #### 二、DECODE函数的基本语法 `DECODE`函数的基本语法如下: ```sql DECODE(input_value, value, result [, value, result] [, default_result]); ``` 其中: - `input_value`:需要被比较的输入值。 - `value`...

    ORACLE 列转行 DECODE函数用法

    DECODE函数的基本语法如下: ```sql DECODE(column, value1, result1, value2, result2, ..., default_result) ``` 这里`column`是要检查的列,`value1`、`value2`等是你想要比较的值,`result1`、`result2`等是对应...

    Decode函数的用法(视图)

    `DECODE`函数是Oracle数据库提供的一种条件判断函数,用于在表达式中执行简单的if-then逻辑。它的基本语法是: ```sql DECODE(expression, value1, result1, [value2, result2, ...], [default]) ``` - `...

    decode函数与case when 的妙用

    在阅读博客文章《decode函数与case when 的妙用》时,作者可能分享了一些具体的示例和使用技巧,帮助我们更好地理解和掌握这两种方法。通过学习和实践,我们可以将这些知识应用于日常的数据库操作,优化数据处理流程...

    使用Oracle的Decode函数进行多值判断

    Decode函数的语法结构如下: 代码如下:decode (expression, search_1, result_1)decode (expression, search_1, result_1, search_2, result_2)decode (expression, search_1, result_1, search_2, result_2, …., ...

    ORACLE DECODE函数在中国式报表统计查询中的组合条件实现

    ### ORACLE DECODE函数在中国式报表统计查询中的组合条件实现 #### 一、引言 在Oracle数据库中,`DECODE`函数是一种非常实用的功能,主要用于条件判断并返回不同的值。它经常被用来进行简单的条件分支处理,尤其...

    SQL语句的DECODE和NVL

    DECODE 函数的语法如下: DECODE(value, if1, then1, if2, then2, if3, then3, ... else) 其中,value 代表某个表的任何类型的任意列或一个通过计算所得的任何结果。当每个 value 值被测试,如果 value 的值为 if1...

    Oracle中DECODE()函数的使用法

    DECODE()函数,它将输入数值与函数中的参数列表相比较,根据输入值返回一个对应值。区别于SQL的其它函数,DECODE函数还能识别和操作空值。本文详细介绍了DECODE函数的语法。

Global site tag (gtag.js) - Google Analytics