------------------------2013-5-14------------------------
oracle數(shù)據(jù)類型
Scalar:
? char(n),nchar(n)
? varchar2(n)
? nvarchar2(n)
? date
? timestamp
? raw
? blob,clob
? nclob,bfile
? long,long raw
? rowid,urowid
Collection:
? varray
? table
Relationship
? ref
PL/SQL 代表Procedural Language/SQL(Procedural Language:過程語言)
它是對SQL的擴(kuò)展
PL/SQL中存在過程構(gòu)造和數(shù)據(jù)庫訪問
優(yōu)點(diǎn):調(diào)用外部函數(shù)和過程。
支持sql,oop
更高生產(chǎn)率,性能,可移植性。
嚴(yán)密的安全性
PL/SQL引擎
過程語句執(zhí)行器(過程語句)和SQL語句執(zhí)行器(SQL語句)。
PL/SQL塊的各個組成部分
? 聲明部分
? 可執(zhí)行部分
? 異常處理部分
PL/SQL塊的結(jié)構(gòu)
? declare
??? declarations? --聲明部分
? begin
??? executable statements? --可執(zhí)行部分
? exception
??? handlers? --異常處理部分
? end;
變量
臨時存儲,賦予新值。
可重用性,易于維護(hù)。
內(nèi)置數(shù)據(jù)類型
標(biāo)量,容納單個值(number,character,date,boolean)
?number類型:binary_integer,number,pls_integer
?number子類型:dec,decimal,double precision,float,integer,int,numeric,real,smallint
?character類型:char,varchar2,raw,long和long raw,rowid和urowid
?區(qū)域字符類型:nchar,nvarchar2
?datetime和interval類型:date,timestamp,
?timestamp with time zone,timestamp with local time zone,
?interval year to month,interval day to second
?boolean類型:true,false,null,只允許對boolean變量執(zhí)行邏輯操作。
復(fù)合,record,varray,nested table
引用,ref cursor,ref操作符
lob,blob,clob,nclob,bfile
屬性的類型:
%type 引用數(shù)據(jù)庫列,變量的類型和數(shù)據(jù)表中的字段的數(shù)據(jù)類型一致。
%rowtype 代表表中的行
記錄類型變量,引用記錄型變量的方法是"記錄變量名.基本類型變量名"
布爾表達(dá)式類型:
數(shù)值型,比較數(shù)值
字符型,比較用引號括起來的序列或單個字符
日期型,比較兩個日期
控制結(jié)構(gòu)
條件控制,if then,if then else,if then elsif和case
迭代控制,while,for
順序控制,goto,null
錯誤處理
預(yù)定義,違反oracle規(guī)則時,將隱式引發(fā)它。
用戶定義,由raise語句顯示引發(fā),只能在pl/sql塊的聲明部分中聲明
pl/sql支持所有sql數(shù)據(jù)類型和ansi標(biāo)準(zhǔn)類型。
------------------------2013-5-15------------------------
1.pl/sql程序的結(jié)構(gòu)
2.基本語法要素,包括常量,基本數(shù)據(jù)類型,復(fù)合數(shù)據(jù)類型,表達(dá)式和函數(shù)。
3.條件控制和循環(huán)控制的流程控制結(jié)構(gòu)
4.事務(wù)處理的commit,rollback和savepoint等3個命令
5.定義,打開和提取游標(biāo)的數(shù)據(jù)
6.無參數(shù)過程和帶參數(shù)過程的創(chuàng)建,查詢,修改和使用方法
7.序列的創(chuàng)建和使用方法
8.定義異常
9.綜合實(shí)例
pl/sql以解釋型方式執(zhí)行,不能編譯成可執(zhí)行文件,脫離支撐環(huán)境執(zhí)行。
每次只能執(zhí)行一條語句,語句以英文的分號;為結(jié)束標(biāo)識。
oracle不像vb,vc程序設(shè)計語言,側(cè)重后臺數(shù)據(jù)庫的管理,提供編程能力較弱。
grant "RESOURCE" to "TEMPUSER";
alter user "TEMPUSER" default role all;
--創(chuàng)建表
create table "TESTTABLE"(
?"RECORDNUMBER" NUMBER(4) NOT NULL,
?"CURRENTDATE" date not null
);
select * from testtable;
--pl/sql添加數(shù)據(jù)
set serveroutput on?? --允許服務(wù)器輸出
declare
? maxrecords constant int:=100;
? i int:=1;
begin
? for i in 1..maxrecords loop
??? insert into TESTTABLE(recordnumber,currentdate)values(i,sysdate);
? end loop;
? dbms_output.put_line('成功錄入數(shù)據(jù)!');
? commit;
end;
成功錄入數(shù)據(jù)!
PL/SQL 過程已成功完成。
sysdate為系統(tǒng)時間函數(shù)
dbms_output為系統(tǒng)默認(rèn)程序包,put_line為包中定義的方法,輸出信息。
對數(shù)據(jù)庫數(shù)據(jù)的更改并沒有直接操作數(shù)據(jù)庫,放在工作區(qū)的內(nèi)存里,直到commit語句執(zhí)行后才發(fā)生永久更改。
常量語法格式
常量名 constant 類型標(biāo)識符 [not null]:=值
括號內(nèi)的not null為可選參數(shù),若選用,表明該常(變)量不能為空值。
--定義常量
declare
? pi constant number(9) :=3.1415926;
begin
? dbms_output.put_line(pi);
? commit;
end;
3
PL/SQL 過程已成功完成。
關(guān)于 varchar2 的最大長度
varchar2有兩個最大長度:一個是在字段類型4000;一個是在PL/SQL中變量類型32767。
boolean 布爾型(true,false,null三者取一)
變量語法格式
變量名 類型標(biāo)識符 [not null]:=值
--定義變量
declare
? age number(3,1) :=26.1;????? --number(3) :=26.1; 輸出26
begin
? dbms_output.put_line(age);
? commit;
end;
--輸出26.1
?
--select * from testtable;
--定義變量,引用列字段類型。
declare
? mydate testtable.currentdate%type:='15-6月 -13';
begin
? dbms_output.put_line(mydate);
? commit;
end;
--記錄類型
set serveroutput on;
declare
? type myrecord is record(
??? myrecordnumber int,
??? mycurrentdate date
? );
? srecord myrecord;
begin
? select * into srecord from testtable where recordnumber = 68;
? dbms_output.put_line(srecord.myrecordnumber);
? dbms_output.put_line(srecord.mycurrentdate);
end;
--在pl/sql程序中,select語句總是和into配合使用,into子句后面就是要被賦值的變量。
--記錄類型
set serveroutput on;
declare
? myrecord testtable%rowtype;
begin
? select * into myrecord from testtable where recordnumber = 88;
? dbms_output.put_line(myrecord.recordnumber);
? dbms_output.put_line(myrecord.currentdate);
end;
一維表類型變量,相當(dāng)于一維數(shù)組。
type 表類型 is table of 類型 index by binary_integer;
表變量名 表類型
-- 一維表類型
declare
? type tabletype1 is table of varchar2(4) index by binary_integer;
? type tabletype2 is table of testtable.recordnumber%type index by binary_integer;
? table1 tabletype1;
? table2 tabletype2;
? begin
??? table1(1) := '大學(xué)';
??? table1(2) := '大專';
??? table2(1) := 88;
??? table2(2) := 55;
? dbms_output.put_line(table1(1) || table2(1));
? dbms_output.put_line(table1(2) || table2(2));
? dbms_output.put_line('總記錄數(shù):' || to_char(table1.count));
? dbms_output.put_line('第一條記錄' || table1.first);
? dbms_output.put_line('最后條記錄' || table1.last);
? dbms_output.put_line('第二條的前一條記錄' || table1.prior(2));
? dbms_output.put_line('第一條的后一條記錄' || table1.next(1));
? end;
count,delete,first,last,next,exists和prior屬性進(jìn)行操作,使用方法為"表變量名.屬性",返回的是數(shù)字。
||是連接字符串的運(yùn)算符。
多維表類型變量,相當(dāng)于多維數(shù)組。
declare
? type tabletype1 is table of testtable%rowtype index by binary_integer;
? table1 tabletype1;
? begin
? select * into table1(1) from testtable where recordnumber=60;
? dbms_output.put_line(table1(1).recordnumber || table1(1).currentdate);
? end;
--table1(1)如果返回多個記錄,也不可以,table1(60)不是代表60個記錄。
數(shù)值表達(dá)式:** 乘方
declare
? result integer;
? begin
??? result := 10+3*4-20+5**2;
??? dbms_output.put_line('返回結(jié)果是:' || to_char(result));
? end;
字符表達(dá)式:就是連接運(yùn)算符 ||
關(guān)系表達(dá)式:= 等于(不是賦值運(yùn)算符:=,like類似于,in在...之中,between在...之間)
關(guān)系型表達(dá)式運(yùn)算符兩邊的表達(dá)式的數(shù)據(jù)類型必須一致。
邏輯表達(dá)式:not邏輯非,or邏輯或,and邏輯與
運(yùn)算的優(yōu)先次序:not,and和or
函數(shù):to_char(字符型),to_date(日期型),to_number(數(shù)值型)
#條件控制#
if..then..end if條件控制
declare
? number1 integer := 90;
? number2 integer := 60;
begin
? if number1 >= number2 then
??? dbms_output.put_line('number1大于等于number2');
? end if;
end;
if..then..else..end if條件控制
declare
? number1 integer := 80;
? number2 integer := 90;
begin
? if number1 >= number2 then
??? dbms_output.put_line('number1大于等于number2');
? else
??? dbms_output.put_line('number1小于number2');
? end if;
end;
if嵌套條件控制
declare
? number1 integer := 80;
? number2 integer := 90;
begin
? if number1 <= number2 then
??? if number1 = number2 then
????? dbms_output.put_line('number1等于number2');
??? else
????? dbms_output.put_line('number1小于number2');
??? end if;
? else
??? dbms_output.put_line('number1大于number2');
? end if;
end;
#循環(huán)控制#
loop .. exit .. end loop循環(huán)控制
declare
? number1 integer := 80;
? number2 integer := 90;
? i integer := 0;
begin
? loop
??? number1 := number1 + 1;
??? if number1 = number2 then
????? exit;
??? else
????? i := i+1;
??? end if;
? end loop;
? dbms_output.put_line('共循環(huán)次數(shù):' || to_char(i));
end;
loop .. exit .. when .. end loop循環(huán)控制
exit when實(shí)際上就相當(dāng)于 if 條件 then
exit
end if;
declare
? number1 integer := 80;
? number2 integer := 90;
? i integer := 0;
begin
? loop
??? number1 := number1 + 1;
??? i := i+1;
??? exit when number1 = number2;
? end loop;
? dbms_output.put_line('共循環(huán)次數(shù):' || to_char(i));
end;
while .. loop .. end loop循環(huán)控制
declare
? number1 integer := 80;
? number2 integer := 90;
? i integer := 0;
begin
? while number1 < number2 loop
??? number1 := number1 + 1;
??? i := i+1;
? end loop;
? dbms_output.put_line('共循環(huán)次數(shù):' || to_char(i));
end;
for .. in .. loop .. end loop循環(huán)控制
declare
? number1 integer := 80;
? number2 integer := 90;
? i integer := 0;
begin
? for i in 1..10 loop
??? number1 := number1 + 1;
? end loop;
? dbms_output.put_line('number1的值:' || to_char(number1));
end;
事務(wù)處理
commit命令,工作區(qū)內(nèi)的修改內(nèi)容才寫入到數(shù)據(jù)庫上,稱為物理寫入,這樣可以保證在任意的客戶機(jī)沒有物理提交修改以前,別的客戶機(jī)讀取的后臺數(shù)據(jù)庫中的數(shù)據(jù)是完整的,一致的。
打開自動提交事務(wù):set auto on;
取消自動提交事務(wù):set auto off;
rollback命令
--查詢--
已選擇100行。
--刪除--
已刪除100行。
--查詢--
未選定行
--rollback--
回退已完成。
--查詢--
已選擇100行。
savepoint命令
創(chuàng)建保存點(diǎn):savepoint 保存點(diǎn)名;
回滾保存點(diǎn):rollback to 保存點(diǎn)名;
insert into testtable values('101','16-5月 -13');
savepoint ins1;
--執(zhí)行刪除操作--
select * from testtable;
rollback to ins1;
--查詢,回滾到刪除之前的狀態(tài)。--
##游標(biāo)##
游標(biāo)是從數(shù)據(jù)表中提取出來的數(shù)據(jù),以臨時表的形式存放在內(nèi)存中,在游標(biāo)中有一個數(shù)據(jù)指針,在初始狀態(tài)下指向的是首記錄,利用fetch語句可以移動該指針,從而對游標(biāo)中的數(shù)據(jù)進(jìn)行各種操作,然后將操作結(jié)果寫回數(shù)據(jù)表中。
cursor 游標(biāo)名 is select 語句;
declare
? rn testtable.recordnumber%type;
? cursor mycursor is select * from testtable where recordnumber > rn;? --定義游標(biāo)
? rs mycursor%rowtype;
begin
? rn := 80;
? open mycursor;? --打開游標(biāo) 1.將符合條件的記錄送入內(nèi)存,2.將指針指向第一條記錄。
? if mycursor%isopen then
??? fetch mycursor into rs;? --提取游標(biāo)
??? if mycursor%found then
????? dbms_output.put_line(to_char(rs.recordnumber) || rs.currentdate);
??? else
????? dbms_output.put_line('沒有數(shù)據(jù)!');
??? end if;
? else
??? dbms_output.put_line('游標(biāo)沒有打開!');
? end if;
? close mycursor;? --關(guān)閉游標(biāo)
end;
--提取游標(biāo)
fetch 游標(biāo)名 into 變量名1,變量名2...;
或
fetch 游標(biāo)名 into 記錄型變量名;
游標(biāo)的屬性
1. %isopen 測試游標(biāo)是否打開。
2. %found 測試前一個fetch語句是否有值,有值將返回true,否則為false。
3. %notfound 是%found屬性的反邏輯,常被用于退出循環(huán)。
4. %rowcount 返回游標(biāo)的數(shù)據(jù)行數(shù),若返回值為0,表明游標(biāo)已經(jīng)打開,但沒有提取出數(shù)據(jù)。
declare
? rn testtable.recordnumber%type;
? cursor mycursor is select * from testtable where recordnumber > rn;
? rs mycursor%rowtype;
begin
? rn := 80;
? open mycursor;
? if mycursor%isopen then
??? fetch mycursor into rs;? --提取游標(biāo)
??? dbms_output.put_line(to_char(mycursor%rowcount));
??? if mycursor%found then
????? dbms_output.put_line(to_char(rs.recordnumber) || rs.currentdate);
??? else
????? dbms_output.put_line('沒有數(shù)據(jù)!');
??? end if;
? else
??? dbms_output.put_line('游標(biāo)沒有打開!');
? end if;
end;
##過程##
共同的特點(diǎn)是沒有名稱,只能存儲為文件,然后通過執(zhí)行文件的方式執(zhí)行,因此稱為無名塊。
與此對應(yīng)的是在pl/sql中也引入了高級程序設(shè)計的一些概念,其中最重要的就是過程。
過程就是高級程序設(shè)計語言中的模塊的概念,將一些內(nèi)部聯(lián)系的命令組成一個個過程,通過參數(shù)在過程之間傳遞數(shù)據(jù)是模塊化設(shè)計思想的重要內(nèi)容。
create or replace procedure 過程名 as? --as關(guān)鍵字代替了無名塊的declare
? 聲明語句段;
begin
? 執(zhí)行語句段;
exception
? 異常處理語句段
end;
create or replace procedure tempprocedure as
? tempdate testtable.currentdate%type;
begin
? select currentdate into tempdate from testtable where recordnumber = 88;
? dbms_output.put_line(to_char(tempdate));
end;
--創(chuàng)建過程是一個數(shù)據(jù)定義命令。
調(diào)用:
begin
tempprocedure;
end;
#參數(shù)類型# 調(diào)用參數(shù)分割用,逗號分割
in參數(shù)
out參數(shù)
inout參數(shù)
create or replace procedure tempprocedure
(
? trn in testtable.recordnumber%type,
? td out testtable.recordnumber%type,
? tn in out testtable.recordnumber%type??? --類型為number(4) or varchar2(20) 會有警告?? 警告: 創(chuàng)建的過程帶有編譯錯誤。
)
as
? tempdate testtable.currentdate%type;
? tempnumber testtable.recordnumber%type;
begin
? select currentdate into tempdate from testtable where recordnumber = trn;
? dbms_output.put_line(to_char(tempdate));
? select recordnumber into tempnumber from testtable where recordnumber = trn;
? dbms_output.put_line(to_char(tempnumber));
? --td := '日期是:' || to_char(tempdate);
?-- tn := '序號是:' || tempnumber;
?td := 1111;
?tn := 2222;
end;
存儲過程的調(diào)用
declare
? trn testtable.recordnumber%type;
? td? testtable.recordnumber%type;
? tn? testtable.recordnumber%type;
begin
? trn := 10;
? td := '';
? tn := '';
? tempprocedure(trn,td,tn);
? dbms_output.put_line(to_char(trn));
? dbms_output.put_line(to_char(td));
? dbms_output.put_line(to_char(tn));
end;
##序列##
CREATE SEQUENCE "NEWLIFEYHJ"."TESTSEQ" INCREMENT BY 1 START WITH
??? 1 MAXVALUE 20 MINVALUE 1 NOCYCLE
??? CACHE 20 NOORDER
--查詢序列值--
select TESTSEQ.nextval from dual;
select TESTSEQ.currval from dual;
insert into testtable values(TESTSEQ.currval,'17-5月 -13');
##異常處理##
系統(tǒng)預(yù)定義異常處理
自定義異常處理
1.定義異常
declare
? 異常名 exception;
2.觸發(fā)異常
? raise 異常名;
3.處理異常
? Exception
? when 異常名1 then
??? 異常處理語句段1;
? when 異常名2 then
??? 異常處理語句段2;
declare
? tempno integer := 90;
? exc exception;
begin
? tempno :=tempno+1;
? if tempno = 91 then
??? raise exc;
? end if;
? exception
? when exc then
??? dbms_output.put_line('自定義異常處理中……');
end;
-->p45/56? 綜合實(shí)例
1.功能設(shè)計
某高校開發(fā)的研究生招生系統(tǒng),要求設(shè)計pl/sql程序?qū)忌某煽償?shù)據(jù)進(jìn)行處理,處理的邏輯是根據(jù)每門專業(yè)課的最低分?jǐn)?shù)線和總分的最低分?jǐn)?shù)線自動將考生歸類為錄取考生,調(diào)劑考生和落選考生。
為此設(shè)計兩個數(shù)據(jù)表,graduate數(shù)據(jù)表存放考生成績,resut數(shù)據(jù)表存放處理結(jié)果,pl/sql程序完成的功能就是將graduate數(shù)據(jù)表的數(shù)據(jù)逐行掃描,根據(jù)分?jǐn)?shù)線進(jìn)行判斷,計算出各科總分,在result數(shù)據(jù)表中將標(biāo)志字段自動添加上"錄取"或"落選"。
2.數(shù)據(jù)表設(shè)計
--研究生表
create table graduate(
? BH NUMBER(10) NOT NULL,
? XM VARCHAR2(10) NOT NULL,
? LB VARCHAR2(10) NOT NULL,
? YINGYU NUMBER(4,1) NOT NULL,
? ZHENGZHI NUMBER(4,1) NOT NULL,
? ZHUANYE1 NUMBER(4,1) NOT NULL,
? ZHUANYE2 NUMBER(4,1) NOT NULL,
? ZHUANYE3 NUMBER(4,1) NOT NULL
);
--結(jié)果表
create table "RESULT"(
? "BH" NUMBER(10) NOT NULL,
? "XM" VARCHAR2(10) NOT NULL,
? "LB" VARCHAR2(10) NOT NULL,
? "YINGYU" NUMBER(4,1) NOT NULL,
? "ZHENGZHI" NUMBER(4,1) NOT NULL,
? "ZHUANYE1" NUMBER(4,1) NOT NULL,
? "ZHUANYE2" NUMBER(4,1) NOT NULL,
? "ZHUANYE3" NUMBER(4,1) NOT NULL,
? "TOTALSCORE" NUMBER(5,1) NOT NULL,
? "FLAG" VARCHAR2(4) NOT NULL
);
3.錄入數(shù)據(jù)
insert into graduate values(2003080520,'張三豐','碩士',55,56,67,78,89);
insert into graduate values(2003060555,'張翠山','碩士',66,78,78,89,92);
insert into graduate values(2003056066,'張無忌','碩士',76,67,89,90,66);
insert into graduate values(2003010989,'趙敏','碩士',45,59,74,66,56);
insert into graduate values(2003050677,'周芷若','碩士',77,67,72,87,66);
insert into graduate values(2003869401,'小昭','碩士',56,67,56,64,34);
insert into graduate values(2003340987,'阿離','碩士',68,93,64,80,56);
insert into graduate values(2003056709,'宋遠(yuǎn)橋','碩士',90,68,81,61,67);
insert into graduate values(2003100894,'殷素素','碩士',69,73,62,70,75);
###
open graduatecursor;
? if graduatecursor%notfound then
??? raise errormessage;
? end if;
? loop
??? fetch graduatecursor into graduaterecord;
??? graduatetotalscore := graduaterecord.yingyu + graduaterecord.zhengzhi + graduaterecord.zhuanye1 + graduaterecord.zhuanye2 + graduaterecord.zhuanye3;
??? if(graduaterecord.yingyu >= tempyingyu and graduaterecord.zhengzhi >= tempzhengzhi and graduaterecord.zhuanye1 >= tempzhuanye1 and graduaterecord.zhuanye2 >= tempzhuanye2 and graduaterecord.zhuanye3 >= tempzhuanye3 and graduatetotalscore >= temptotalscore) then
????? graduateflag := '錄取';
??? else
????? graduateflag := '落選';
??? end if;
??? exit when graduatecursor%notfound;
??? insert into result values(graduaterecord.bh,graduaterecord.xm,graduaterecord.lb,graduaterecord.yingyu,graduaterecord.zhengzhi,graduaterecord.zhuanye1,graduaterecord.zhuanye2,graduaterecord.zhuanye3,graduatetotalscore,graduateflag);
? end loop;
? close graduatecursor;
? commit;
? exception
? when errormessage then
? dbms_output.put_line('無法打開數(shù)據(jù)表');
create or replace procedure graduateprocess
--(
? --tempzhengzhi in graduate.zhengzhi%type,
? --tempyingyu in graduate.yingyu%type,
? --tempzhuanye1 in graduate.zhuanye1%type,
? --tempzhuanye2 in graduate.zhuanye2%type,
? --tempzhuanye3 in graduate.zhuanye3%type,
? --temptotalscore in result.totalscore%type
--)
as
? --graduaterecord graduate%rowtype;
? --graduatetotalscore result.totalscore%type;
? --graduateflag varchar2(4);
? --cursor gradutecursor is select * from graduate;
? --errormessage exception;
begin
?
end;
###
4.程序設(shè)計
完整存儲過程
/*定義過程參數(shù)*/
create or replace procedure graduateprocess
(
? tempzhengzhi in graduate.zhengzhi%type,
? tempyingyu in graduate.yingyu%type,
? tempzhuanye1 in graduate.zhuanye1%type,
? tempzhuanye2 in graduate.zhuanye2%type,
? tempzhuanye3 in graduate.zhuanye3%type,
? temptotalscore in result.totalscore%type
)
as
/*定義graduaterecord為記錄型變量,臨時存放通過游標(biāo)從graduate數(shù)據(jù)表中提取的記錄*/
? graduaterecord graduate%rowtype;
/*定義graduatetotalscore為數(shù)值型變量,統(tǒng)計總分*/?
? graduatetotalscore result.totalscore%type;
/*定義graduateflag為字符型變量,根據(jù)結(jié)果放入"落選"或"錄取",然后寫入數(shù)據(jù)表result*/
? graduateflag varchar2(4);
/*定義游標(biāo)graduatecursor,存放的是所有的graduate數(shù)據(jù)表中的記錄*/?
? cursor graduatecursor is select * from graduate;
/*定義異常處理errormessage*/?
? errormessage exception;
/*開始執(zhí)行*/?
begin
? /*打開游標(biāo)*/
? open graduatecursor;
? /*如果游標(biāo)沒有數(shù)據(jù),激活異常處理*/
? if graduatecursor%notfound then
??? raise errormessage;
? end if;
? /*游標(biāo)有數(shù)據(jù),指針指向第一條記錄,每執(zhí)行fetch命令,就自動下移,循環(huán)執(zhí)行到記錄提取完畢為止*/
? loop
??? fetch graduatecursor into graduaterecord;
??? /*計算總分*/
??? graduatetotalscore := graduaterecord.yingyu + graduaterecord.zhengzhi + graduaterecord.zhuanye1 + graduaterecord.zhuanye2 + graduaterecord.zhuanye3;
??? /*判斷單科和總分是否滿足錄取要求,若滿足,graduateflag變量值為"錄取",否則為"落選"*/
??? if(graduaterecord.yingyu >= tempyingyu and graduaterecord.zhengzhi >= tempzhengzhi and graduaterecord.zhuanye1 >= tempzhuanye1 and graduaterecord.zhuanye2 >= tempzhuanye2 and graduaterecord.zhuanye3 >= tempzhuanye3 and graduatetotalscore >= temptotalscore) then
????? graduateflag := '錄取';
??? else
????? graduateflag := '落選';
??? end if;
??? /*當(dāng)游標(biāo)數(shù)據(jù)提取完畢后,退出循環(huán)*/
??? exit when graduatecursor%notfound;
??? /*向結(jié)果數(shù)據(jù)表result中插入處理后的數(shù)據(jù)*/
??? insert into result values(graduaterecord.bh,graduaterecord.xm,graduaterecord.lb,graduaterecord.yingyu,graduaterecord.zhengzhi,graduaterecord.zhuanye1,graduaterecord.zhuanye2,graduaterecord.zhuanye3,graduatetotalscore,graduateflag);
? end loop;
? /*關(guān)閉游標(biāo)*/
? close graduatecursor;
? /*提交結(jié)果*/
? commit;
? /*異常處理,提示錯誤信息*/
? exception
? when errormessage then
? dbms_output.put_line('無法打開數(shù)據(jù)表');
? /*程序執(zhí)行結(jié)束*/
end;
5.執(zhí)行結(jié)果,程序調(diào)用。
主程序mainprocess
/*定義6個入口變量,分別對應(yīng)graduate數(shù)據(jù)表中的專業(yè)課和總分分?jǐn)?shù)線*/
declare
? score1 number(4,1);
? score2 number(4,1);
? score3 number(4,1);
? score4 number(4,1);
? score5 number(4,1);
? scoretotal number(5,1);
/*將分?jǐn)?shù)線賦值,在這里修改各值就代表不同的分?jǐn)?shù)線*/
begin
? score1 := 50;
? score2 := 56;
? score3 := 60;
? score4 := 62;
? score5 := 64;
? scoretotal := 325;
/*調(diào)用處理過程*/
? graduateprocess(score1,score2,score3,score4,score5,scoretotal);
end;
綜合運(yùn)用pl/sql的設(shè)計要素,就可以設(shè)計出很多復(fù)雜的處理程序,這也是DBA的一項重要任務(wù)。
Oracle Class6. PL/SQL 簡介(數(shù)據(jù)類型,邏輯比較,控制結(jié)構(gòu),錯誤處理)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
