PL-SQL 存儲(chǔ)過(guò)程與函數(shù)_第1頁(yè)
PL-SQL 存儲(chǔ)過(guò)程與函數(shù)_第2頁(yè)
PL-SQL 存儲(chǔ)過(guò)程與函數(shù)_第3頁(yè)
PL-SQL 存儲(chǔ)過(guò)程與函數(shù)_第4頁(yè)
PL-SQL 存儲(chǔ)過(guò)程與函數(shù)_第5頁(yè)
已閱讀5頁(yè),還剩1頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

PL/SQL存儲(chǔ)過(guò)程與函數(shù)1、存儲(chǔ)過(guò)程存儲(chǔ)過(guò)程的參數(shù)形式參數(shù)和實(shí)際參數(shù),例如,有如下一個(gè)存儲(chǔ)過(guò)程,該過(guò)程接收一個(gè)作家代碼和一個(gè)工資值,將該作家的工資改為接收到的工資值。Java代碼create

or

replace

procedure

updateauths(

p_authscode

auths.author_code%type,

p_authssalary

auths.salary%type)

as

begin

update

auths

set

salary=p_authssalary

where

author_code=p_authscode;

commit;

end

updateauths;

下面的PL/SQl塊調(diào)用updateauths存儲(chǔ)過(guò)程,將代碼為A00011的作家的工資改為350元。Java代碼declare

v_authorcode

auths.author_code%type:='A00011';

v_salary

auths.salary%type:=350;

begin

updateauths(v_authorcode,v_salary);

end;

v_authorcode、v_salary作為參數(shù)傳遞到存儲(chǔ)過(guò)程updateauths中,這些參數(shù)是實(shí)際參數(shù),簡(jiǎn)稱實(shí)參。

p_authscode、p_authssalary就是形式參數(shù),簡(jiǎn)稱形參。

參數(shù)定義中,IN、OUT和INOUT代表參數(shù)的三種不同模式:

IN:當(dāng)調(diào)用存儲(chǔ)過(guò)程時(shí),該模式的形參接收對(duì)應(yīng)實(shí)參的值,并且該是只讀的,即不能被修改。默認(rèn)為IN。

OUT:該形參被認(rèn)為只能寫,既只能為其賦值。在存儲(chǔ)過(guò)程中不能讀它的值。返回時(shí),將該形參值傳給相應(yīng)的實(shí)參。

INOUT:都允許。Java代碼create

or

replace

procedure

updateauthssalary(

p_author_code

in

out

auths.author_code%type,

p_salary

in

number,

p_name

out

%type)

is

v_salary_temp

number;

--定義存儲(chǔ)過(guò)程中的局部變量

begin

select

salary

into

v_salary_temp

from

auths

where

author_code=p_author_code;

if

v_salary_temp<300

then

update

auths

set

salary=p_salary

where

author_code=p_author_code;

end

if;

select

name

into

p_name

from

auths

where

author

code=p_author_code;

end

updateauthssalary;

(1)參數(shù)的數(shù)據(jù)類型在定義一個(gè)存儲(chǔ)過(guò)程參數(shù)時(shí),不能指定CHAR類型和VARCHAR2類型形參的長(zhǎng)度,也不能指定NUMBER形參的精度和標(biāo)度。這些約束由實(shí)參來(lái)傳遞。

例如,下面的存儲(chǔ)過(guò)程定義不合法,將產(chǎn)生一個(gè)編譯錯(cuò)誤:Java代碼create

or

replace

procedure

proc_auths(

--參數(shù)定義了類型長(zhǎng)度,將產(chǎn)生編譯錯(cuò)誤。

p_code

in

out

varchar2(6),

p_salary

out

number(8,2))

as

begin

select

salary

into

p_salary

from

auths

where

author_code=p_code;

end

proc_auths;

修改上面存儲(chǔ)過(guò)程的定義為:Java代碼create

or

replace

procedure

proc_auths(

--參數(shù)定義了類型長(zhǎng)度,將產(chǎn)生編譯錯(cuò)誤。

p_code

in

out

varchar2,

p_salary

out

number)

as

begin

select

salary

into

p_salary

from

auths

where

author_code=p_code;

end

proc_auths;

p_code的長(zhǎng)度約束和p_salary的精度,標(biāo)度約束由實(shí)參傳遞。Java代碼delcare

v_code

varchar2(6);

v_salary

number(8,2);

begin

v_code:='A00001';

proc_auths(v_code,v_salary);

end;

注意,如果使用%type為參數(shù)定義類型,那么該參數(shù)具有定義在形參上而不是通過(guò)實(shí)參傳遞的數(shù)據(jù)長(zhǎng)度。Java代碼create

or

replace

procedure

query_salary(

p_code

in

out

auths.author_code%type,

p_salary

out

auths.salary%type)

as

--那么由于author_code的長(zhǎng)度為6,因此p_code的長(zhǎng)度也為6。

(2)參數(shù)的傳值方式位置表示法、名稱表示法

如有這樣的存儲(chǔ)過(guò)程Java代碼create

or

replace

procedure

insert_auths(

p_code

auths.author_code%type,

p_name

%type,

p_sex

auths.sex%type,

p_birthdate

auths.birthdate%type)

as

下面進(jìn)行兩種方法的調(diào)用:Java代碼declare

v_code

varchar2(6);

v_name

varchar2(12);

v_sex

number(1);

v_birthdate

date;

begin

v_code:='A00021';

v_name:='張';

v_sex:=1;

v_birthdate:='5-seq-70';

--實(shí)參的位置順序與形參的位置順序相對(duì)應(yīng)。---位置表示法

insert_auths(v_code,v_name,v_sex,v_birthdate);

--實(shí)參名與形參名對(duì)應(yīng),這樣就可以重新排列參數(shù)的先后順序。---命名表示法

end;

注意,位置表示法和命名表示法在一些調(diào)用中也可以混合使用。但是,如果出現(xiàn)第一個(gè)用命名表示法的參數(shù)時(shí),后面的參數(shù)也必須使用命名表示法傳值。

(3)參數(shù)的缺省值

如可以這樣:

p_entry_date_timeauths.entry_date_time%type:sysdate,

p_sexauths.sex%typedefault1

2、創(chuàng)建函數(shù)函數(shù)與存儲(chǔ)過(guò)程非常類似,都有三種模式的參數(shù)。它們都可以存儲(chǔ)在數(shù)據(jù)庫(kù)中(當(dāng)然過(guò)程與函數(shù)也可以不在于數(shù)據(jù)庫(kù)中),并且在塊中調(diào)用。

與存儲(chǔ)過(guò)程不同,存儲(chǔ)過(guò)程只能作為一個(gè)PL/SQL語(yǔ)句調(diào)用,而函數(shù)作為表達(dá)式的一部分調(diào)用。并且它們的定義、可執(zhí)行、異常處理部分是不同的。

例如,如作家表中男作家或女作家的工資在200元以上的人數(shù)大于百分之七十,則下面的函數(shù)返回TRUE,否則返回FALSE:Java代碼create

or

replace

function

salarystat(

p_sex

auths.sex%type)

return

boolean

is

v_currentsexauthors

number;

v_maxauthors

number;

v_returnvalue

boolean;

v_percent

constant

number:=70;

begin

--獲得滿足條件的作家的最大數(shù)。

select

count(author_code)

into

v_maxauthors

from

auths

where

sex=p_sex

and

salary>=200;

select

count(author_code)

into

v_currentsexauthors

from

auths

where

sex=p_sex;

if(v_maxauthors/v_currentsexauthors*100)>v_percent

then

v_returnvalue:=true;

else

v_returnvalue:=false;

end

if;

return

v_returnvalue;

end

salarystat;

下面進(jìn)行調(diào)用:Java代碼declare

cursor

c_auths

is

select

distinct

sex

from

auths;

begin

for

v_authsrecord

in

c_auths

loop

if

salarystat(v_authsrecord.sex)

then

update

auths

set

salary=salary-50

where

sex=v_authsrecord.sex;

end

if;

end

loop;

end;

return也可以用在存儲(chǔ)過(guò)程中。在這種情況下,它沒有參數(shù)。當(dāng)執(zhí)行了不帶參數(shù)的return語(yǔ)句后,立刻將控制返回到調(diào)用環(huán)境,并將OUT和INOUT模式的形參的當(dāng)前值傳給實(shí)參,然后繼續(xù)執(zhí)行調(diào)用存儲(chǔ)過(guò)程后的語(yǔ)句。在使用函數(shù)與存儲(chǔ)過(guò)程時(shí),一般情況下,如果只有一個(gè)返回值,則使用函數(shù);如果有多個(gè)返回值則使用存儲(chǔ)過(guò)程。盡管函數(shù)的參數(shù)可以是OUT模式,但是一般不這樣使用。3、刪除過(guò)程與函數(shù)dropprocedureprocedurename;dropfunctionfunctionname;4、庫(kù)存子程序和局部子程序前面的子程序都是存儲(chǔ)在數(shù)據(jù)庫(kù)中的子程序,即庫(kù)存子程序。這些子程序是由ORACLE命令創(chuàng)建的,并可在其它的PL/SQL塊中調(diào)用。它們?cè)趧?chuàng)建時(shí)要進(jìn)行編譯,并將編譯后的代碼存儲(chǔ)在數(shù)據(jù)庫(kù)中。當(dāng)子程序被調(diào)用時(shí),編譯后的代碼從數(shù)據(jù)庫(kù)中讀出并執(zhí)行。

一個(gè)子程序也可以在塊的定義部分創(chuàng)建,這樣的子程序被叫作局部子程序。

下面定義了一個(gè)局部函數(shù)formatname:Java代碼declare

cursor

c_allauthors

is

select

name,sex

from

auths;

v_formattedname

varchar2(60);

function

formatname(p_name

in

varchar2,p_sex

in

number)

return

varchar2

is

v_sex

varchar2(16);

begin

if

p_sex=1

then

v_sex:='男';

else

v_sex:='女';

end

if;

return

p_name||'('||v_sex||')';

end

formatname;

begin

for

v_authsrecord

in

c_allauthors

l

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論