存儲過程就是對業務關係複雜的sql語句的封裝,sql語句以程序塊的形式被封裝到了數據庫中,寫好之後無論是java、還是.net都可以向使用sql一樣使用這就是數據庫開發。
oracle 存儲過程的基本語法
create or replace procedure testPro(
arg1 in number, --in 傳遞參數
arg2 out number, --out 返回參數
arg3 in out number -- in out 傳遞和返回參數
)
create or replace procedure testPro(
inarg in number,
outarg out number,
inoutarg in out number
)is v_localvaliable number;
begin
--分配inarg給v_localvaliable
v_localvaliable :=inarg;
--分配7給inarg這是不合法的,因爲聲明是in
inarg:=7;
--分配7給outarg這是合法的,因爲聲明是out
outarg:=7;
--分配outarg給v_localvaliable 合法
v_localvaliable:=outarg;
--分配inoutarg給v_localvaliable 合法 in out
v_localvaliable:=inoutarg;
--分配7給inoutarg 合法 in out
inoutarg:=7;
end testPro;
create or replace procedure delemp(
v_empno in emp.empno%type
)as
no_result exception;
begin
delete from emp where empno=v_empno;
if SQL%notfound then
raise no_result;
end if;
dbms_output.put_line('編碼爲'||v_empno||'的員工已被除名!');
exception
when no_result then
dbms_output.put_line('你需要的數據不存在');
when others then
dbms_output.put_line('發生其它錯誤');
end delemp;
1. 使用%TYPE
在許多情況下,PL/SQL變量可以用來存儲在數據庫表中的數據。在這種情況下,變量應該擁有與表列相同的類型。例如,students表的first_name列的類型爲VARCHAR2(20),我們可以按照下述方式聲明一個變量:
DECLARE
v_FirstName VARCHAR2(20);
但是如果first_name列的定義改變了會發生什麼(比如說表改變了,first_name現在的類型變爲VARCHAR2(25))?那就會導致所有使用這個列的PL/SQL代碼都必須進行修改。如果你有很多的PL/SQL代碼,這種處理可能是十分耗時和容易出錯的。
這時,你可以使用”%TYPE”屬性而不是將變量類型硬性編碼。
例如:
DECLARE
v_FirstName students.first_name%TYPE;
通過使用%TYPE,v_FirstName變量將同students表的first_name列的類型相同(可以理解爲將兩者邦定起來)。
--創建存儲過程
create or replace procedure testPro
(
param1 in number, --參數
param2 in number
)is
variable1 integer :=0; --變量
variable2 date ;
begin
end testPro
--將select查詢結果存入到變量中,可以同時將多個列存儲到多個變量中,必須有一條記錄否則
--拋出異常(如果沒有記錄拋出NO_DATA_FOUND)
begin
select col1,col2 into 變量1,變量2 from table1 where xxx;
exception when NO_DATA_FOUND then xxx;
end;
--if 判斷
if v_test=1 then
begin
do someing
end;
end if;
--while 循環
while v_test=1 loop
begin
xxxxx
end ;
end loop;
--變量賦值
v_test:=123;
--用for in 使用 cursor
...
is
cursor cur is select * from xxx;
begin
for cur_result in cur loop
begin
v_sum:=cur_result.列名1+cur_result.列名2;
end;
end loop;
end ;
--帶參數的cursor
cursor c_user(c_id number) is select name from user where typeid=c_id;
open c_user(變量值);
loop
fetch c_user into v_name;
exit fetch c_user%notfound;
do someing
end loop;
close c_user;
關於oracle存儲過程的若干問題備忘
select a.appname from appinfo as a;-- 錯誤
select af.keynode from APPFOUNDATION af where af.appid=aid and af.foundationid=fid;-- 沒有into,編譯報錯,提示:Compilation
Error: PLS-00428: an INTO clause is expected in this SELECT statement
select af.keynode into kn from APPFOUNDATION af where af.appid=appid and af.foundationid=foundationid;-- 運行階段報錯,提示
ORA-01422:exact fetch returns more than requested number of rows
id varchar2(50) primary key not null,
vcount number(8) not null,
bid varchar2(50) not null -- 外鍵
);
fcount:=0;
end if;
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
CallableStatement cs = session
.connection()
.prepareCall("{call modifyapppnumber_remain(?)}");
cs.setString(1, foundationid);
cs.execute();
return null;
}
});
原文:http://www.cnblogs.com/happyday56/archive/2007/07/05/806830.html