學習《PLSQL開發指南》筆記——條件和序列控制
一、三種條件語句
1、if-then-end if
2、if-then-else-end if
2、if-then-elsif-then-else-end if
注意:1、條件是boolean型,null和任何型別的運算都是false;
2、來自資料庫的值,要關注null問題:is null,nvl;
3、多條件,也需要排除null值,and或者or,只需要其中一個條件true或者false,就可以判斷結果。
begin
if null or null
then
dbms_output.put_line('null值的運算結果是true!');
else
dbms_output.put_line('null值得運算結果是false!');
end if;
end;
二、case 語句和case表示式
1、case語句
declare
v_case varchar2(10) := 'ding';
begin
case v_case
when 'd' then
dbms_output.put_line('選中的字母是:' || v_case);
when 'ding' then
dbms_output.put_line('選中的字母是:' || v_case);
else
dbms_output.put_line('沒有選中字母');
end case;
end;
2、case 語句
declare
v_case varchar2(10) := 'd';
begin
case
when v_case = 'd' then
dbms_output.put_line('選中的字母是:' || v_case);
when v_case = 'ding' then
dbms_output.put_line('選中的字母是:' || v_case);
else
dbms_output.put_line('沒有選中字母');
end case;
end;
3、case 語句
declare
v_num int := 1500;
begin
case
when v_num >= 1000 then
case
when v_num >= 1200 then
dbms_output.put_line('傳入的資料大於1200!');
when v_num >= 1500 then
dbms_output.put_line('傳入的資料大於1500!');
when v_num >= 4000 then
dbms_output.put_line('傳入的資料大於4000!');
end case;
when v_num < 1000 then dbms_output.put_line('傳入的值小於1000!');
end case;
end;
4、case 表示式:轉換結果,可以給函式傳引數:case when then else end;
declare
t_bl boolean := true;
f_bl boolean := false;
n_bl boolean;
function v_func(v_flag in boolean) return varchar2 is
begin
return case v_flag when true then 'True' when false then 'Else' else 'null' end;
end;
begin
dbms_output.put_line(v_func(t_bl));
dbms_output.put_line(v_func(f_bl));
dbms_output.put_line(v_func(n_bl));
end;
case 表示式和case語句的區別
1、case 語句以end case 結尾;case 表示式以end結尾;
2、case 語句then 以後有分號;case 表示式沒有;
3、case 語句中沒有else 會報錯誤;case表示式沒有else則返回null;
4、case 表示式以end結尾可以參與到運算中;
最後的例子:
declare
dump_mesg varchar2(20);
begin
select case
when dummy = 'X' then
'dual is OK!'
else
'dual is messed up!'
end
into dump_mesg
from dual;
dbms_output.put_line(dump_mesg);
end;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22275400/viewspace-1317918/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- React學習筆記-條件渲染React筆記
- Vue學習筆記(三)條件渲染和迴圈渲染Vue筆記
- SpringBoot學習筆記13——MybatisPlus條件查詢Spring Boot筆記MyBatis
- mysql,where條件查詢等學習筆記MySql筆記
- MYSQL學習筆記6: DQL條件查詢(where)MySql筆記
- 【學習筆記】Prufer 序列筆記
- RxJava 學習筆記 -- 條件操作符和布林操作符RxJava筆記
- Mybatis學習筆記 3:Mybatis 多種條件查詢MyBatis筆記
- 《JAVA學習指南》學習筆記Java筆記
- Python學習筆記3(條件語句+迴圈語句)Python筆記
- Android 開發學習筆記Android筆記
- TCP/IP學習筆記之協議和郵件TCP筆記協議
- Go 條件語句 - Go 學習記錄Go
- springboot 開發學習筆記1Spring Boot筆記
- React入門指南(學習筆記)React筆記
- Go 入門指南學習筆記Go筆記
- Python 反序列化漏洞學習筆記Python筆記
- NDK學習筆記-NDK開發流程筆記
- Web開發學習筆記——HTTP 概述Web筆記HTTP
- JS開發步驟學習筆記JS筆記
- iOS學習筆記07 運動事件和遠端控制iOS筆記事件
- 強化學習-學習筆記15 | 連續控制強化學習筆記
- Golang學習筆記-IDE安裝指南Golang筆記IDE
- MySQL筆記 10 條件邏輯MySql筆記
- iOS學習筆記15 序列化、偏好設定和歸檔iOS筆記
- 【區塊鏈學習】《區塊鏈學習指南》學習筆記區塊鏈筆記
- Swoft 學習筆記之控制器筆記
- ESP32 開發筆記(四)LVGL控制元件學習 3 Button 按鈕控制元件筆記控制元件
- Java雜記3—流程控制之條件Java
- [CTF/Web] PHP 反序列化學習筆記WebPHP筆記
- 作業系統學習筆記之初識程式和程式控制作業系統筆記
- dubbo學習筆記---dubbo開發實戰筆記
- 學習Rust 條件語句Rust
- [學習筆記]SQL server完全備份指南筆記SQLServer
- ESP32 開發筆記(四)LVGL控制元件學習 15 Keyboard 鍵盤控制元件 X筆記控制元件
- Web 開發學習筆記(3) — 申請和部署HTTPS證書Web筆記HTTP
- Flutter學習指南:UI佈局和控制元件FlutterUI控制元件
- PHP 反序列化漏洞入門學習筆記PHP筆記
- Java_EE企業級開發學習筆記——spring學習筆記第二章Java筆記Spring