oracle if 和 case語句的使用
oracle if語句和case語句的使用例子
--------------------if----------------------------
set serveroutput on
declare
v_n1 number(2):=-9;begin
if v_n1=0 then
dbms_output.put_line('這個值是0');
elsif v_n1=1 then
dbms_output.put_line('這個值是1');
elsif v_n1=2 then
dbms_output.put_line('這個值是2');
else
if v_n1<0 then
dbms_output.put_line('是一個負數');
else
dbms_output.put_line('是一個正數');
end if;
end if;
end;
---------------------case-------------------------------------
declare
v_date date:=to_date('2017-05-13 12:14:15','YYYY-MM-DD HH24:MI:SS');
v_c varchar2(20);
BEGIN
v_c:=to_char(v_date,'d');
case v_c
when '1' then
DBMS_OUTPUT.PUT_LINE('Today is Sunday');
when '2' then
DBMS_OUTPUT.PUT_LINE('Today is Monday');
when '3' then
DBMS_OUTPUT.PUT_LINE('Today is Tuesday');
when '4' then
DBMS_OUTPUT.PUT_LINE('Today is Wendesday');
when '5' then
DBMS_OUTPUT.PUT_LINE('Today is Thursday');
when '6' then
DBMS_OUTPUT.PUT_LINE('Today is Friday');
else
DBMS_OUTPUT.PUT_LINE('Today is Saturday');
end case;
END;
--第二種寫法
declare
v_course number;
v_list varchar2(100);
begin
select a into v_course from a;
case
when v_course >=90 then v_list:='A';
when v_course >=80 then v_list:='B';
when v_course >=70 then v_list:='C';
when v_course >=60 then v_list:='D';
else v_list:='F';
end case;
DBMS_OUTPUT.PUT_LINE(v_list);
end;