Oracle/PLSQL, the decode function has the functionality of an IF-THEN-ELSE statement.
The syntax for the decode function is:
decode( expression , search , result [, search , result]... [, default] )
expression is the value to compare.
search is the value that is compared against expression.
result is the value returned, if expression is equal to search.
default is optional. If no matches are found, the decode will return default. If default is omitted, then the decode statement will return null (if no matches are found).
Applies To:
* Oracle 9i, Oracle 10g, Oracle 11g
For example:
You could use the decode function in an SQL statement as follows:
SELECT supplier_name,
decode(supplier_id, 10000, 'IBM',
10001, 'Microsoft',
10002, 'Hewlett Packard',
'Gateway') result
FROM suppliers;
The above decode statement is equivalent to the following IF-THEN-ELSE statement:
IF supplier_id = 10000 THEN
result := 'IBM';
ELSIF supplier_id = 10001 THEN
result := 'Microsoft';
ELSIF supplier_id = 10002 THEN
result := 'Hewlett Packard';
ELSE
result := 'Gateway';
END IF;
The decode function will compare each supplier_id value, one by one.
分享到:
相关推荐
Oracle PL/SQL 语法详细手册是一本全面介绍Oracle数据库中SQL和PL/SQL语言的参考资料。手册分为两大部分,第一部分专注于SQL语法,第二部分深入到PL/SQL编程。 **第一部分 SQL语法部分** 1. **CREATE TABLE 语句**...
SQL>select text from all_source where owner=user and name=upper('&plsql_name'); 三、查看数据库的SQL 1、查看表空间的名称及大小 select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size ...
create or replace function fn_get_user_count return number is begin return (select count(*) from users); end fn_get_user_count; ``` ##### 3.4 PL/SQL编写格式 - PL/SQL代码需遵循一定的格式,例如...
CREATE OR REPLACE FUNCTION get_employee_info( p_id IN NUMBER ) RETURN VARCHAR2 IS v_name EMPLOYEE.NAME%TYPE; BEGIN SELECT NAME INTO v_name FROM EMPLOYEE WHERE ID = p_id; RETURN v_name; END; / `...
根据提供的文件信息,我们可以整理出以下关于Oracle数据库中的SQL语句和函数的相关知识点: ### 经典SQL语句收集(ORACLE) #### 文件概述 该文件包含了一系列经典的Oracle SQL语句及函数,主要目的是用于学习和...
DECODE(l.type, 'TM', 'TABLELOCK', 'TX', 'ROWLOCK', NULL) LOCK_LEVEL, o.owner, o.object_name, o.object_type, s.sid, s.serial#, s.terminal, s.machine, s.program, s.osuser FROM v$sessions s, v$lock ...
SQL>select text from all_source where wner=user and name=upper('&plsql_name'); 三、查看数据库的SQL 1、查看表空间的名称及大小 select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size ...
- **AGGREGATE FUNCTION**:如DECODE或CASE表达式与MAX、MIN、SUM等聚合函数结合使用,适用于8i,9i,10g 及以后版本。例如,将表`t_row_col`的行转换回原列: ```sql SELECT id, MAX(decode(cn, 'c1', cv, NULL)) AS...
xzp_base_function.INSERT_ERRORS('liudecai', 150, 'ITS_ALLPLANSUM_PKG', 'autoLevel'); end; ``` 这段代码中使用了`FOR UPDATE`子句,这意味着在执行查询的过程中,数据库将会自动锁定被访问的数据行,直到事务...