您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 廊坊分类信息网,免费分类信息发布

oracle/plsql case条件语句的用法

2024/2/22 11:11:53发布21次查看
本文章要介绍关于oracle/plsql case条件语句的用法,它和mysql mssql都差不多,好了费话不说多了需要的学同可以看看吧。
本文章要介绍关于oracle/plsql case条件语句的用法,它和mysql mssql都差不多,好了费话不说多了需要的学同可以看看吧。
语句语法
 代码如下 复制代码
case  [ expression ]
  when condition_1 then result_1
  when condition_2 then result_2
  ...
  when condition_n then result_n
  else result
end
expression 可选的。它的价值,你比较的条件清单。 (即:condition_1,condition_2,... condition_n)
condition_1到condition_n都必须是相同的数据类型。条件评估中列出的顺序。一个条件是一旦发现是真实的,case语句将返回的结果和不评价任何进一步的条件。
result_1到result_n都必须是相同的数据类型。这是返回的值一个条件是,一旦发现是真的。
注意:
如果没有条件为真,那么case语句将返回在else子句里的值。
如果省略了else子句和任何条件发现是真的,那么case语句将返回null。
最多可以有255在case语句比较。时,每个...条款被认为是2比较。
applies to:
oracle 9i, oracle 10g, oracle 11g
实例
你可以使用case语句在sql语句如下:(包括表达式子句)
 代码如下 复制代码
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;
或者你可以写sql语句,使用这样的情况下声明:(省略了表达式子句)
 代码如下 复制代码
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;
上述两个案例语句以下的if - then- else语句是等价的:
 代码如下 复制代码
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;
case语句会比较每一位业主的价值,一个接一个。
需要注意的一点是,在case语句的else子句是可选的的。你可以省略。让我们看看上面的sql语句与else子句省略。
您的sql语句如下所示:
 代码如下 复制代码
select table_name,
case owner
  when 'sys' then 'the owner is sys'
  when 'system' then 'the owner is system'
end
from all_tables;
实例
下面就是一个例子,演示了如何使用case语句来比较不同条件下:
代码如下 复制代码
select
case
  when a   when d end
from suppliers;
廊坊分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录