`

Oracle 中条件分歧总结

阅读更多
Oracle 中条件分歧总结:
* Decode
* IF--THEN--ELSIF---END IF
* CASE WHEN---END CASE

1.DECODE介绍
   In 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;

Refer to:http://www.techonthenet.com/oracle/functions/index.php

2. IF-THEN-ELSE Statement
There are three different syntaxes for these types of statements.

Syntax #1: IF-THEN

IF condition THEN
         {...statements...}
END IF;


Syntax #2: IF-THEN-ELSE

IF condition THEN
         {...statements...}
ELSE
         {...statements...}
END IF;


Syntax #3: IF-THEN-ELSIF

IF condition THEN
         {...statements...}
ELSIF condition THEN
         {...statements...}
ELSE
         {...statements...}
END IF;


Here is an example of a function that uses the IF-THEN-ELSE statement:

CREATE OR REPLACE Function IncomeLevel
     ( name_in IN varchar2 )
     RETURN varchar2
IS
     monthly_value number(6);
     ILevel varchar2(20);

     cursor c1 is
        select monthly_income
        from employees
        where name = name_in;

  BEGIN

open c1;
fetch c1 into monthly_value;
close c1;

IF monthly_value <= 4000 THEN
     ILevel := 'Low Income';

ELSIF monthly_value > 4000 and monthly_value <= 7000 THEN
     ILevel := 'Avg Income';

ELSIF monthly_value > 7000 and monthly_value <= 15000 THEN
     ILevel := 'Moderate Income';

ELSE
     ILevel := 'High Income';

END IF;

  RETURN ILevel;

END;

3.Case Statement
Starting in Oracle 9i, you can use the case statement within an SQL statement. It has the functionality of an IF-THEN-ELSE statement.

The syntax for the case statement is:

CASE  [ expression ]
  WHEN condition_1 THEN result_1
  WHEN condition_2 THEN result_2
  ...
  WHEN condition_n THEN result_n
  ELSE result
END

expression is optional. It is the value that you are comparing to the list of conditions. (ie: condition_1, condition_2, ... condition_n)

condition_1 to condition_n must all be the same datatype. Conditions are evaluated in the order listed. Once a condition is found to be true, the case statement will return the result and not evaluate the conditions any further.

result_1 to result_n must all be the same datatype. This is the value returned once a condition is found to be true.


Note:

If no condition is found to be true, then the case statement will return the value in the ELSE clause.

If the ELSE clause is omitted and no condition is found to be true, then the case statement will return NULL.

You can have up to 255 comparisons in a case statement. Each WHEN ... THEN clause is considered 2 comparisons.


Applies To:

Oracle 9i, Oracle 10g, Oracle 11g

For example:

You could use the case statement in an SQL statement as follows: (includes the expression clause)

select table_name,
CASE owner
  WHEN 'SYS' THEN 'The owner is SYS'
  WHEN 'SYSTEM' THEN 'The owner is SYSTEM'
  ELSE 'The owner is another value'
END
from all_tables;

Or you could write the SQL statement using the case statement like this: (omits the expression clause)

select table_name,
CASE
  WHEN owner='SYS' THEN 'The owner is SYS'
  WHEN owner='SYSTEM' THEN 'The owner is SYSTEM'
  ELSE 'The owner is another value'
END
from all_tables;
The above two case statements are equivalent to the following IF-THEN-ELSE statement:

IF owner = 'SYS' THEN
     result := 'The owner is SYS';

ELSIF owner = 'SYSTEM' THEN
    result := 'The owner is SYSTEM'';

ELSE
    result := 'The owner is another value';

END IF;


The case statement will compare each owner value, one by one.


One thing to note is that the ELSE clause within the case statement is optional. You could have omitted it. Let's take a look at the SQL statement above with the ELSE clause omitted.

Your SQL statement would look as follows:

select table_name,
CASE owner
  WHEN 'SYS' THEN 'The owner is SYS'
  WHEN 'SYSTEM' THEN 'The owner is SYSTEM'
END
from all_tables;

With the ELSE clause omitted, if no condition was found to be true, the case statement would return NULL.


For Example:

Here is an example that demonstrates how to use the case statement to compare different conditions:

select
CASE
  WHEN a < b THEN 'hello'
  WHEN d < e THEN 'goodbye'
END
from suppliers;


Frequently Asked Questions

Question:  Can you create a case statement that evaluates two different fields? I want to return a value based on the combinations in two different fields.

Answer:  Yes, below is an example of a case statement that evaluates two different fields.

select supplier_id,
CASE
  WHEN supplier_name = 'IBM' and supplier_type = 'Hardware' THEN 'North office'
  WHEN supplier_name = 'IBM' and supplier_type = 'Software' THEN 'South office'
END
from suppliers;
分享到:
评论

相关推荐

    oracle 学习心得与总结

    数据库对象是Oracle中的基本组成部分,包括表、视图、索引、存储过程、函数、触发器等。这些对象共同构成了数据库的结构,并支持数据的存储、查询和操作。 2. **表(约束)**: 表是数据库中存储数据的主要结构,...

    Oracle常用函数实战总结.xlsx、Oracle 11g数据库管理员指南.pdf、Oracle开发实战经典.pdf

    内容:总结了Oracle数据库中常用的函数及其实战应用。这些函数可能包括字符串处理函数、数值计算函数、日期和时间函数、转换函数等。 2.用途:对于Oracle数据库的使用者来说,这个Excel表格可以作为一个快速查找和...

    oracle dba 面试题总结

    "Oracle DBA 面试题总结" Oracle DBA 面试题总结是 Oracle 数据库管理员需要掌握的重要知识点的汇总。本文将从 SQL 调优、执行计划、索引、绑定变量、执行计划稳定性、排序相关内存等方面对 Oracle DBA 面试题进行...

    Oracle DBA常用命令总结

    Oracle DBA常用命令总结 , 包含 复杂权限控制等等

    casewhen多条件oracle-casewhen(casewhen同时满足多条件).pdf

    Oracle CASE WHEN 多条件语句详解 CASE WHEN 语句是 SQL 中的一种条件语句,用于实现多个...CASE WHEN 语句是 Oracle 数据库中的一种非常有用的语句,可以帮助我们实现多个条件的判断和执行,提高查询效率和读取性。

    Oracle非常全的学习总结

    在Oracle中,SQL不仅可以用来查询数据,还可以创建、修改和删除数据库对象。基础的SQL语句包括SELECT用于查询,INSERT用于插入记录,UPDATE用于更新记录,DELETE用于删除记录。进阶的SQL概念有联接(JOIN)、子查询...

    oracle数据库知识点总结.txt

    中软工作总结一:oracle

    oracle精品实例,练习总结

    在实例中,我们可能看到如何使用SELECT语句进行复杂查询,JOIN操作连接多个表,以及如何使用WHERE子句进行条件筛选。 2. **PL/SQL**:Oracle特有的过程式语言,结合了SQL的查询能力与编程语言的功能,用于编写存储...

    oracle游标的总结oracle游标的总结

    Oracle 游标是 Oracle 数据库中的一种重要概念,用于查询数据库,获取记录集合(结果集)的指针。游标可以看作是一个临时表,你可以对其每一行的数据进行任意的操作。本文将对 Oracle 游标的概念、分类、声明方式、...

    oracle总结oracle总结面试必备

    游标(Cursor)在Oracle中用于处理单条记录,尤其在循环处理大量数据时。通过DECLARE、OPEN、FETCH、CLOSE等步骤,可以逐行读取查询结果。游标在编写复杂的PL/SQL代码和处理动态数据时非常有用。 四、触发器 触发器...

    oracle个人总结笔记

    本人自己总结的尚学堂oracle笔记,希望能派上用场,本资源不收资源分,大家可以随意下载

    Oracle数据库命令 个人总结

    "Oracle数据库命令个人总结" Oracle数据库命令个人总结是徐博文在2014年3月整理的关于Oracle数据库的命令和心得的总结。下面是从该总结中提取的重要知识点: 1. 查询数据库数据文件的基本字段信息:使用DESC DBA_...

    Oracle学习笔记(Oracle知识点总结)

    Oracle知识点总结,适合初学者,平时多联系,放在手机上,当做电子书来看。

    oracle知识点总结.txt

    oracle知识点总结.txt 个人对oracle的一些总结

    Oracle SQL性能优化总结

    简单的整理了一些Oracle性能优化方面的知识。 供大家参考学习。

    Oracle数据库中ORDER BY排序和查询按IN条件的顺序输出

    本文将深入探讨`ORDER BY`子句在Oracle中的排序机制,以及查询时根据`IN`条件顺序输出的方法。 首先,`ORDER BY`用于指定查询结果的排序方式,可以基于一个或多个列进行升序(ASC)或降序(DESC)排序。然而,...

    ORACLE应用经验总结

    ORACLE应用经验总结,是常年工作在oracle第一线资深人士总结文档

    Java操作Oracle的基本方式总结

    Java操作Oracle的基本方式总结的方法

    Oracle分析函数使用总结

    Oracle分析函数使用总结Oracle分析函数使用总结Oracle分析函数使用总结Oracle分析函数使用总结

    Oracle到Mysql数据库迁移总结

    Oracle到Mysql数据库迁移总结Oracle到Mysql数据库迁移总结Oracle到Mysql数据库迁移总结Oracle到Mysql数据库迁移总结

Global site tag (gtag.js) - Google Analytics