Oracle case語句語法簡(jiǎn)介
Oracle case語句是我們最常用的語句之一,下面就為您介紹Oracle case語句的語法,并舉例說明,希望對(duì)您能夠有所幫助。
當(dāng)執(zhí)行多重條件分支語句時(shí),使用Oracle case語句更加簡(jiǎn)潔、而且效率也更好。Oracle case語句處理多重條件分支語句有兩種方法,***種方法是使用單一選擇符進(jìn)行等值比較。第二種方法是使用多種條件進(jìn)行非等值比較。
1、使用單一選擇符進(jìn)行等值比較
當(dāng)執(zhí)行Oracle case語句執(zhí)行多重條件分支時(shí),如果條件選擇符完全相同,并且條件表達(dá)式為相同條件選擇,那么可以選擇單一選擇符進(jìn)行等值比較,語法如下:
case 條件選擇符
when 條件值表達(dá)式1 then 要執(zhí)行的操作1;
when 條件值表達(dá)式2 then 要執(zhí)行的操作2;
。。。。。。。
else
要執(zhí)行的操作。
end case;
示例如下:
- declare
- v_count number;
- begi
- select count(*) into v_count from cip_temps;
- case v_count
- when 1 then
- dbms_output.put_line('when 1操作___v_cont的值:'|| v_count);
- when 5 then
- dbms_output.put_line('when 5操作___v_count的值:'|| v_count);
- when 10 then
- dbms_output.put_line('when 10操作____v_count的值:'|| v_count);
- else
- dbms_output.put_line('else操作____v_cout的值:'||v_count);
- end case;
- end;
- /
2、case使用多種條件進(jìn)行比較
如果選擇多個(gè)條件進(jìn)行不同比較時(shí),那么必須在when子句中指定比較條件,語法如下:
case
when 條件值表達(dá)式1 then 要執(zhí)行的操作1;
when 條件值表達(dá)式2 then 要執(zhí)行的操作2;
。。。。。。。
else
要執(zhí)行的操作。
end case;
示例如下:
- declare
- v_count number;
- begin
- select count(*) into v_count from cip_temps;
- case
- when v_count>10 then
- dbms_output.put_line('when 1操作___v_cont的值:'|| v_count);
- when v_count>5 then
- dbms_output.put_line('when 5操作___v_count的值:'|| v_count);
- when v_count>4 then
- dbms_output.put_line('when 10操作____v_count的值:'|| v_count);
- else
- dbms_output.put_line('else操作____v_cout的值:'||v_count);
- end case;
- end;
【編輯推薦】