当前位置: 首页 > 图文教程 > 数据库 > Oracle > oracle的case函数控制结构DECODE()函数

Oracle
Oracle常用dump命令,记录一下备查。
Oracle存储过程之数据库中获取数据实例
Oracle存储过程入门学习基本语法
java.sql.SQLException: 内部错误: Unable to construct a Datum from the specified input
Oracle 函数大全[字符串函数,数学函数,日期函数]
Oracle 自增(auto increment) 或 标识字段的建立方法
oracle 存储过程加密的方法
Oracle针对数据库某一行进行操作的时候,如何将这一行加行锁
Oracle 忘记密码的找回方法
Oracle 数据库导出(exp)导入(imp)说明
oracle 常见等待事件及处理方法
Oracle9i 动态SGA,PGA特性探索
PDO取Oracle lob大字段,当数据量太大无法取出的问题的解决办法
ORACLE 数据库RMAN备份恢复
用Mimer Validator检查SQL查询
oracle执行cmd的实现方法
ORACLE 正则解决初使化数据格式不一致
Oracle 触发器的使用小结
oracle 时间格式的调整
Oracle 10g的DBA无法登录解决方案

Oracle 中的 oracle的case函数控制结构DECODE()函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-30   浏览: 65 ::
收藏到网摘: n/a

  oracle的case函数

  create sequence STUDENT_SEQUENCE;

  CREATE TABLE students (

  id NUMBER(5) PRIMARY KEY,

  stu_name VARCHAR2(20),

  score NUMBER(10,0),

  grade varchar2(2));

  INSERT INTO students (id, stu_name, score, grade)

  VALUES (student_sequence.NEXTVAL, ’Scott’, 98,null);

  INSERT INTO students (id, stu_name, score, grade)

  VALUES (student_sequence.NEXTVAL, ’Margaret’, 88,null);

  INSERT INTO students (id, stu_name, score, grade)

  VALUES (student_sequence.NEXTVAL, ’Joanne’, 75,null);

  INSERT INTO students (id, stu_name, score, grade)

  VALUES (student_sequence.NEXTVAL, ’Manish’, 66,null);

  update students

  set grade =

  case when score > 90 then ’a’

  when score > 80 then ’b’

  when score > 70 then ’c’

  else ’d’ end

  ----------------

  oracle的case结构

  begin

  case when 2<1 then

  dbms_output.put_line(’y’);

  else

  dbms_output.put_line(’n’);

  end case;

  end;

  /

  这个case when ... end case 和上边的case when ... end是不同的。

  2007-6-3 10:56:51 Oracle中DECODE()函数的使用法

  DECODE()函数,它将输入数值与函数中的参数列表相比较,根据输入值返回一个对应值。函数的参数列表是由若干数值及其对应结果值组成的若干序偶形式。当然,如果未能与任何一个实参序偶匹配成功,则函数也有默认的返回值。

  decode函数比较表达式和搜索字,如果匹配,返回结果;如果不匹配,返回default值;如果未定义default值,则返回空值。

  区别于SQL的其它函数,DECODE函数还能识别和操作空值。

  语法:DECODE(control_value,value1,result1[,value2,result2…][,default_result]);

  control _value试图处理的数值。DECODE函数将该数值与后面的一系列的偶序相比较,以决定返回值。

  value1是一组成序偶的数值。如果输入数值与之匹配成功,则相应的结果将被返回。对应一个空的返回值,可以使用关键字NULL于之对应

  result1 是一组成序偶的结果值。

  default_result 未能与任何一个值匹配时,函数返回的默认值。

  例如: 小虫网络技术http://www.chinaccna.com

  selectdecode( x , 1 , ‘x is 1 ’, 2 , ‘x is 2 ’, ‘others’) from dual

  当x等于1时,则返回‘x is 1’。

  当x等于2时,则返回‘x is 2’。

  否则,返回others’。

  需要,比较2个值的时候,可以配合SIGN()函数一起使用。

  SELECT DECODE( SIGN(5 -6), 1 ’Is Positive’, -1, ’Is Nagative’, ’Is Zero’)

  同样,也可以用CASE实现:

  SELECT CASE SIGN(5 - 6)

  WHEN 1 THEN ’Is Positive’

  WHEN -1 THEN ’Is Nagative’

  ELSE ’Is Zero’ END

  FROM DUAL

  此外,还可以在Order by中使用Decode。

  例如:表table_subject,有subject_name列。要求按照:语、数、外的顺序进行排序。这时,就可以非常轻松的使用Decode完成要求了。

  select * from table_subject order by decode(subject_name, ’语文’, 1, ’数学’, 2, , ’外语’,3)