轉自:http://blog.csdn.net/pg_roger/article/details/8877782
1 create or replace procedure 存儲過程名
2 is
3 begin
4 null;
5 end;
行1:
create or replace procedure 是一個sql語句通知oracle資料庫去建立一個叫做skeleton存儲過程, 如果存在就覆寫它;
行2:
is關鍵詞表明後面将跟随一個pl/sql體。
行3:
begin關鍵詞表明pl/sql體的開始。
行4:
null pl/sql語句表明什麼事都不做,這句不能删去,因為pl/sql體中至少需要有一句;
行5:
end關鍵詞表明pl/sql體的結束
create or replace procedure 存儲過程名(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
else
raise 異常名(no_data_found);
end if;
exception
when others then
rollback;
end;
注意事項:
1, 存儲過程參數不帶取值範圍,in表示傳入,out表示輸出
類型可以使用任意oracle中的合法類型。
2, 變量帶取值範圍,後面接分号
3, 在判斷語句前最好先用count(*)函數判斷是否存在該條操作記錄
4, 用select 。。。into。。。給變量指派
5, 在代碼中抛異常用 raise+異常名
create or replace procedure存儲過程名
(
--定義參數
is_ym in char(6) ,
the_count out number,
)
--定義變量
vs_msg varchar2(4000); --錯誤資訊變量
vs_ym_beg char(6); --起始月份
vs_ym_end char(6); --終止月份
vs_ym_sn_beg char(6); --同期起始月份
vs_ym_sn_end char(6); --同期終止月份
--定義遊标(簡單的說就是一個可以周遊的結果集)
cursor cur_1 is
select 。。。
from 。。。
where 。。。
group by 。。。;
begin
--用輸入參數給變量賦初值,用到了oralce的substr to_char add_months
to_date 等很常用的函數。
vs_ym_beg := substr(is_ym,1,6);
vs_ym_end := substr(is_ym,7,6);
vs_ym_sn_beg := to_char(add_months(to_date(vs_ym_beg,'yyyymm'), -12),'yyyymm');
vs_ym_sn_end := to_char(add_months(to_date(vs_ym_end,'yyyymm'), -12),'yyyymm');
--先删除表中特定條件的資料。
delete from 表名 where ym = is_ym;
--然後用内置的dbms_output對象的put_line方法列印出影響的記錄行數,其中用到一個系統變量sql%rowcount
dbms_output.put_line('del上月記錄='||sql%rowcount||'條');
insert into表名(area_code,ym,cmcode,rmb_amt,usd_amt)
select area_code,is_ym,cmcode,sum(rmb_amt)/10000,sum(usd_amt)/10000
from bgd_area_cm_m_base_t
where ym >= vs_ym_beg
and ym <= vs_ym_end
group by area_code,cmcode;
dbms_output.put_line('ins當月記錄='||sql%rowcount||'條');
--周遊遊标處理後更新到表。周遊遊标有幾種方法,用for語句是其中比較直覺的一種。
for rec in cur_1 loop
update 表名
set rmb_amt_sn = rec.rmb_amt_sn,usd_amt_sn = rec.usd_amt_sn
where area_code = rec.area_code
and cmcode = rec.cmcode
and ym = is_ym;
end loop;
commit;
--錯誤處理部分。others表示除了聲明外的任意錯誤。sqlerrm是系統内置變量儲存了目前錯誤的詳細資訊。
exception
when others then
vs_msg := 'error in xxxxxxxxxxx_p('||is_ym||'):'||substr(sqlerrm,1,500);
rollback;
--把目前錯誤記錄進日志表。
insert into log_info(proc_name,error_info,op_date)
values('xxxxxxxxxxx_p',vs_msg,sysdate);
commit;
return;
oracle存儲過程文法
1 、判斷語句:
if 比較式 then begin end; end if;
create or replace procedure test(x in number) is
if x >0 then
begin
x := 0 - x;
end;
end if;
if x = 0 then
begin
x: = 1;
end;
end test;
2 、for 循環
for ... in ... loop
-- 執行語句
(1) 循環周遊遊标
create or replace procedure test() as
cursor cursor is select name from student; name varchar(20);
for name in cursor loop
dbms_output.putline(name);
end;
(2) 循環周遊數組
create or replace procedure test(vararray in mypackage.testarray) as
--( 輸入參數vararray 是自定義的數組類型,定義方式見标題6)
i number;
i := 1; -- 存儲過程數組是起始位置是從1 開始的,與java 、c 、c++ 等語言不同。因為在oracle 中本是沒有數組的概念的,數組其實就是一張
-- 表(table), 每個數組元素就是表中的一個記錄,是以周遊數組時就相當于從表中的第一條記錄開始周遊
for i in 1..vararray.count loop
dbms_output.putline('the no.'|| i || 'record in vararray is:'||vararray(i));
end loop;
3 、while 循環
while 條件語句 loop
e.g
create or replace procedure test(i in number) as
while i < 10 loop
begin
i:= i + 1;
end test;
4 、數組
首先明确一個概念:oracle 中本是沒有數組的概念的,數組其實就是一張表(table), 每個數組元素就是表中的一個記錄。
使用數組時,使用者可以使用oracle 已經定義好的數組類型,或可根據自己的需要定義數組類型。
(1) 使用oracle 自帶的數組類型
x array; -- 使用時需要需要進行初始化
e.g:
create or replace procedure test(y out array) is
x array;
begin
x := new array();
y := x;
(2) 自定義的數組類型 ( 自定義資料類型時,建議通過建立package 的方式實作,以便于管理)
create or replace package mypackage is
public type declarations type info is record( name varchar(20), y number);
type testarray is table of info index by binary_integer;
-- 此處聲明了一個testarray 的類型資料,其實其為一張存儲info 資料類型的table 而已,及testarray 就是一張表,有兩個字段,一個是name ,一個是y 。需要注意的是此處使用了index by binary_integer 編制該table 的索引項,也可以不寫,直接寫成:type testarray is
table of info ,如果不寫的話使用數組時就需要進行初始化:vararray mypackage.testarray; vararray := new mypackage.testarray();
end testarray;
5. 遊标的使用 oracle 中cursor 是非常有用的,用于周遊臨時表中的查詢結果。其相關方法和屬性也很多,現僅就常用的用法做一二介紹:
(1)cursor 型遊标( 不能用于參數傳遞)
create or replace procedure test() is
cusor_1 cursor is select std_name from student where ...; --cursor 的使用方式1 cursor_2 cursor;
select class_name into cursor_2 from class where ...; --cursor 的使用方式2
可使用for x in cursor loop .... end loop; 來實作對cursor 的周遊
(2)sys_refcursor 型遊标,該遊标是oracle 以預先定義的遊标,可作出參數進行傳遞
create or replace procedure test(rscursor out sys_refcursor) is
cursor sys_refcursor;
name varhcar(20);
open cursor for select name from student where ... --sys_refcursor 隻能通過open 方法來打開和指派
loop
fetch cursor into name --sys_refcursor 隻能通過fetch into 來打開和周遊 exit when cursor%notfound; --sys_refcursor 中可使用三個狀态屬性: ---%notfound( 未找到記錄資訊) %found( 找到記錄資訊) ---%rowcount( 然後目前遊标所指向的行位置)
dbms_output.putline(name);
rscursor := cursor;
執行個體
下面寫一個簡單的例子來對以上所說的存儲過程的用法做一個應用:
現假設存在兩張表,一張是學生成績表(studnet) ,字段為:stdid,math,article,language,music,sport,total,average,step
一張是學生課外成績表(out_school), 字段為:stdid,parctice,comment
通過存儲過程自動計算出每位學生的總成績和平均成績,同時,如果學生在課外課程中獲得的評價為a ,就在總成績上加20 分。
create or replace procedure autocomputer(step in number) is
rscursor sys_refcursor;
commentarray mypackage.myarray;
math number;
article number;
language number;
music number;
sport number;
total number;
average number;
stdid varchar(30);
record mypackage.stdinfo;
i := 1;
get_comment(commentarray); -- 調用名為get_comment() 的存儲過程擷取學生課外評分資訊
open rscursor for select stdid,math,article,language,music,sport from student t where t.step = step;
fetch rscursor into stdid,math,article,language,music,sport; exit when rscursor%notfound;
total := math + article + language + music + sport;
for i in 1..commentarray.count loop
record := commentarray(i);
if stdid = record.stdid then
begin
if record.comment = 'a' then
begin
total := total + 20;
go to next; -- 使用go to 跳出for 循環
end;
end if;
end;
end if;
<<continue>> average := total / 5;
update student t set t.total=total and t.average = average where t.stdid = stdid;
end autocomputer;
-- 取得學生評論資訊的存儲過程
create or replace procedure get_comment(commentarray out mypackage.myarray) is
rs sys_refcursor ;
comment varchar(1);
open rs for select stdid,comment from out_school
fetch rs into stdid,comment; exit when rs%notfound;
record.stdid := stdid;
record.comment := comment;
recommentarray(i) := record;
i:=i + 1;
end get_comment;
-- 定義數組類型myarray
create or replace package mypackage is begin
type stdinfo is record(stdid varchar(30),comment varchar(1));
type myarray is table of stdinfo index by binary_integer;
end mypackage;
byebye