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

Oracle
Oracle数据库技术(32)
Oracle数据库技术(33)
Oracle数据库技术(34)
Oracle数据库技术(35)
Oracle数据库技术(36)
Oracle数据安全面面观
Oracle数据操作和控制语言详解
Oracle数据库数据对象分析
解析Oracle 8i/9i的计划稳定性
使用Oracle实现实时通信
Oracle数据库中索引的维护
Oracle数据库游标使用大全
Oracle9i中监视索引的使用
在Oracle9i中使用多种Block Size
监控Oracle数据库的常用shell脚本
Performance Improvement Tips for Oracle on UNIX
Raw Partitions and Windows NT
How to use OS commands to diagnose Database Performance issues?
Raw Devices and Oracle - 20 Common Questions and Answers
Monitor Oracle Resource Consumption in UNIX

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-30   浏览: 66 ::
收藏到网摘: 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)