概念了解
???????存儲(chǔ)過程Procedure是一組為了完成特定功能的SQL語句集合缭保,經(jīng)編譯后存儲(chǔ)在數(shù)據(jù)庫中,用戶通過指定存儲(chǔ)過程的名稱并給出參數(shù)來執(zhí)行蝙茶。
???????存儲(chǔ)過程中可以包含邏輯控制語句和數(shù)據(jù)操縱語句艺骂,它可以接受參數(shù)、輸出參數(shù)隆夯、返回單個(gè)或多個(gè)結(jié)果集以及返回值彻亲。
???????由于存儲(chǔ)過程在創(chuàng)建時(shí)即在數(shù)據(jù)庫服務(wù)器上進(jìn)行了編譯并存儲(chǔ)在數(shù)據(jù)庫中,所以存儲(chǔ)過程運(yùn)行要比單個(gè)的SQL語句塊要快吮廉。同時(shí)由于在調(diào)用時(shí)只需用提供存儲(chǔ)過程名和必要的參數(shù)信息,所以在一定程度上也可以減少網(wǎng)絡(luò)流量畸肆、簡(jiǎn)單網(wǎng)絡(luò)負(fù)擔(dān)宦芦。
紙上談兵
存儲(chǔ)過程
CREATE OR REPLACE PROCEDURE 存儲(chǔ)過程名
IS
BEGIN
NULL;
END;
行1: CREATE OR REPLACE PROCEDURE 是一個(gè)SQL語句通知Oracle數(shù)據(jù)庫去創(chuàng)建一個(gè)叫做skeleton存儲(chǔ)過程, 如果存在就覆蓋它;
行2: IS關(guān)鍵詞表明后面將跟隨一個(gè)PL/SQL體。
行3: BEGIN關(guān)鍵詞表明PL/SQL體的開始轴脐。
行4: NULL PL/SQL語句表明什么事都不做调卑,這句不能刪去,因?yàn)镻L/SQL體中至少需要有一句;
行5: END關(guān)鍵詞表明PL/SQL體的結(jié)束
存儲(chǔ)過程創(chuàng)建語法:
create or replace procedure 存儲(chǔ)過程名(param1 in type大咱,param2 out type)
as
變量1 類型(值范圍); --vs_msg VARCHAR2(4000);
變量2 類型(值范圍);
Begin
Select count(*) into 變量1 from 表A where列名=param1恬涧;
If (判斷條件) then
Select 列名 into 變量2 from 表A where列名=param1;
Dbms_output碴巾。Put_line(‘打印信息’);
Elsif (判斷條件) then
Dbms_output溯捆。Put_line(‘打印信息’);
Else
Raise 異常名(NO_DATA_FOUND);
End if;
Exception
When others then
Rollback;
End;
注意事項(xiàng):
1, 存儲(chǔ)過程參數(shù)不帶取值范圍厦瓢,in表示傳入提揍,out表示輸出
類型可以使用任意Oracle中的合法類型。
2煮仇, 變量帶取值范圍劳跃,后面接分號(hào)
3, 在判斷語句前最好先用count(*)函數(shù)判斷是否存在該條操作記錄
4浙垫, 用select 刨仑。。夹姥。into杉武。。佃声。給變量賦值
5艺智, 在代碼中拋異常用 raise+異常名
全軍出擊
實(shí)參形參:
create or replace procedure myDemo04(name in varchar,age in int)
as
begin
dbms_output.put_line('name='||name||', age='||age);
end;
declare
name varchar(10);
age int;
begin
name:='xiaoming';
age:=18;
myDemo04(name=>name,age=>18);--此時(shí)不能myDemo04(name=>name,18),不能完成調(diào)用圾亏。
end;
注;在調(diào)用存儲(chǔ)過程時(shí)十拣,=>前面的變量為存儲(chǔ)過程的形參且必須于存儲(chǔ)過程中定義的一致封拧,而=>后的參數(shù)為實(shí)際參數(shù)。當(dāng)然也不可以不定義變量保存實(shí)參
in夭问,out參數(shù)問題
create or replace procedure myDemo05(name out varchar,age in int)
as
begin
dbms_output.put_line('age='||age);
select 'seaco' into name from dual;
end;
declare
name varchar(10);
age int;
begin
myDemo05(name=>name,age=>10);
dbms_output.put_line('name='||name);
end;
注:in代表輸入泽西,out用于輸出
crud
create or replace procedure mydemo07(ids in int, username in varchar,userpass in varchar, userage in int)
as
begin
-- insert into students(id,username,userpass,userage)
-- values(ids,username,userpass,userage); --增
-- delete from students where id=ids; --刪
-- update students set userage=100 where id=ids;--改
commit;
end;
begin
mydemo07(10,'a','b','17');
end;
---------------------------
create or replace procedure mydemo08(ids in int, age out int)
as
begin
select userage into age from students where id=ids; --查
commit;
end;
declare
ids int;
age int;
begin
ids:=1;
myDemo08(ids=>ids,age=>age);
dbms_output.put_line('age='||age);
end;
for循環(huán)
create or replace procedure mydemo09
as
begin
for stu in (select * from students) loop
if (stu.id<5) then
dbms_output.put_line(stu.id);
end if;
end loop;
commit;
end;
begin
mydemo09();
end;
while循環(huán)
create or replace procedure test_while_loop as
n_count number := 0;
begin
while n_count < 10 loop
dbms_output.put_line(n_count);
n_count := n_count + 1;
end loop;
end;
begin
test_while_loop();
end;