日期:2014-05-16 浏览次数:20535 次
CASE selector WHEN value1 THEN action1; WHEN value2 THEN action2; WHEN value3 THEN action3; ….. ELSE actionN; END CASE;
DECLARE temp VARCHAR2(10); v_num number; BEGIN v_num := &i; temp := CASE v_num WHEN 0 THEN 'Zero' WHEN 1 THEN 'One' WHEN 2 THEN 'Two' ELSE NULL END; dbms_output.put_line('v_num = '||temp); END;
CASE WHEN (boolean_condition1) THEN action1; WHEN (boolean_condition2) THEN action2; WHEN (boolean_condition3) THEN action3; …… ELSE actionN; END CASE;
DECLARE a number := 20; b number := -40; tmp varchar2(50); BEGIN tmp := CASE WHEN (a>b) THEN 'A is greater than B' WHEN (a<b) THEN 'A is less than B' ELSE 'A is equal to B' END; dbms_output.put_line(tmp); END;
SELECT grade, COUNT (CASE WHEN sex = 1 THEN 1 /*sex 1为男生,2位女生*/ ELSE NULL END) 男生数, COUNT (CASE WHEN sex = 2 THEN 1 ELSE NULL END) 女生数 FROM students GROUP BY grade;