Oracle (1) 基本語句操作 之 if-else\for\while迴圈
目錄
示例前提
模仿一個通用的學校的師生資訊資料庫,使用sql語句已經建好了如下表,表結構如下,資料自己隨意啦:
- student 學生資訊表:
sno(學號) | sname(姓名) | ssex(性別) | sage(年齡) | deptno(所在院系序號) |
- teacher 教師資訊表:
tno(編號) | tname(姓名) | tsex(性別) | grad(畢業院校) | title(職位) | deptno(所在院系序號) |
- score 學生成績表:
sno(學號) | cno(課程號) | grade(成績) |
- course
cno(課程號) | cname(課程名) | ccredit(學分) | institute(所屬院系序號) |
- sdept 學院資訊表:
deptno(院系序號) | dname(院系名稱) | address(院系地址) | telephone(負責人電話) |
【註釋】jdk版本和"oracle sql developer"不符合,當向表中插入資料的時候,可能導致實際值與最大值不匹配,“值過多”等報錯,利用語句:alter table student modify sname char(10) 可以為student(報錯表)修改姓名欄位長度。
最基礎的固定結構
【通用結構】
declare
宣告
begin
具體執行方法
end;
【例題】最簡單的輸出
--是oracle的註釋符號
declare
x int:=0; --定義變數格式:變數名 資料型別:=初值;
begin
x:=6; --改變x的值
dbms_output.put_line(x); --輸出x
end;
【輸出】6
【註釋】如果輸出端沒有任何顯示,請執行程式碼“set serveroutput on”開啟輸出。
怎麼把變數和sql語句關聯起來?
【導言】在sql語句中加入into 變數名即可
【例題】查詢student學生表中有多少行【輸出:16】
declare
x int:=0;
begin
select count(*) into x from student; --在sql語句中加入into 變數名即可
dbms_output.put_line(x); --輸出總記錄行數x
end;
常見的變數宣告格式
【普通】:就是上面例子的 x int:=0; 這種格式
【查詢某一行的某一列】:假如要查詢成績表score中的分數grade這一列,x為變數名
- 變數宣告格式為:x score.grade%type;
【查詢某一行】:假如要查詢成績表score中的每一行,x為變數名
- 變數宣告格式為:x score%rowtype;
【例題】查詢學生高天的學號,查詢於多的學號、姓名、年齡
declare
m student.sage%type; --可以m int:=0;這樣普通定義
x student%rowtype;
begin
select sage into m from student where sname='高天';
select * into x from student where sname='於多';
dbms_output.put_line(m);
dbms_output.put_line(x.sno||x.sname||x.sage);
end;
【輸出】
20
0802010206 於多 19
If-else結構
【通用結構】
if 條件1 then
執行語句
elsif 條件2 then
執行語句
else
執行語句
end if;
【例題】查詢學生高天的c語言成績在哪個等級段
declare
x score.grade%type;
begin
select grade into x from score where sno=(select sno from student where sname='高天')
and cno=(select cno from course where cname='C語言');
if x>80 and x<100 then
dbms_output.put_line(x||'優秀');
elsif x>60 and x<80 then
dbms_output.put_line(x||'及格');
else
dbms_output.put_line(x||'不及格');
end if;
end;
【輸出】45不及格
【註釋】在oracle中,<>代表“不等於”。
for、while迴圈的使用
【通用結構1:結合if-else語句】(迴圈段和if語句順序不同結果不同)
loop
if-else語句(略);
需要迴圈的程式段
end loop;
【通用結構2:結合while語句】
while 條件
loop
需要迴圈的程式段
end loop;
【通用結構3:結合for語句】
for 條件
loop
需要迴圈的程式段
end loop;
【例題1.1】輸出從1+2+....+5的結果
declare
x int:=1 ;
total int:=0;
begin
loop
if x>5 then
exit; --退出,相當於break
end if;
total:=total+x;
x:=x+1;
end loop;
dbms_output.put_line(total);
end;
【輸出】15
【解析】注意if的位置,上述程式碼相當於c語言中的while和for迴圈,先判斷再執行。
【例題1.2】1.1的程式碼可以替換成:
declare
x int:=1 ;
total int:=0;
begin
loop
total:=total+x;
x:=x+1;
if x>5 then
exit;
end if;
end loop;
dbms_output.put_line(total);
end;
【輸出】15 (15:1+2+3+4+5)
【解析】這種 if 放在迴圈程式碼段後面的,類似於c語言中的do-while,先執行再判斷。然而還需要注意一個細節:
【例題1.3】上述程式碼的細節問題
declare
x int:=1 ;
total int:=0;
begin
loop
x:=x+1;
total:=total+x;
if x>5 then
exit;
end if;
end loop;
dbms_output.put_line(total);
end;
【輸出】 20(20:2+3+4+5+6)
【解析】注意到細節了麼?total:=total+x; 和 x:=x+1;互換了位置,迴圈次數雖然一樣,但是結果不同。
【例題1.4】結合for\while實現1+2+...+5
declare
x int:=1;
total int:=0;
begin
while x<=5 --該行可以替換成 for i in 1..5
loop
total:=total+x;
x:=x+1;
end loop;
dbms_output.put_line(total);
end;
【輸出】15
【例題2.1】遍歷score表中的所有資料,並且顯示每一行資料
declare
v_score score%rowtype; --表示表格中的某一行
v_count int; --當前在多少行
v_n int default 1; --這個表的總行數
begin
select count(*) into v_count from score;
loop
select sno,cno,grade into v_score from(
select rownum as num,score.* from score)
where num=v_n;
dbms_output.put_line(v_score.sno||v_score.cno||v_score.grade);
v_n:=v_n+1;
if v_n>v_count then
exit;
end if;
end loop;
end;
【註釋】雖然可以直接使用sql語句實現,但是為了說明變數和sql如何關聯,以及oracle的語法,就寫複雜了點
【例題2.2】使用for迴圈實現遍歷score表
declare
v_score score%rowtype;
v_count int;
begin
select count(*) into v_count from score;
for i in 1..v_count
loop
select sno,cno,grade into v_score from(
select rownum as num,score.* from score)
where num=i;
dbms_output.put_line(v_score.sno||v_score.cno||v_score.grade);
end loop;
end;