存儲過程 有用
--- 給某員工漲工資(打印漲前的工資和漲後的工資) 可以改成商品的漲價前和漲價後
create or replace procedure updateSal(eno in number ,psal in number)
is
oldsal number;
newsal number;
begin
-- 打印漲前的工資
select sal into oldsal from emp where empno = eno;
dbms_output.put_line(‘漲前的工資:‘||oldsal);
-- 漲工資
update emp set sal = sal + psal where empno = eno;
commit;
-- 打印漲後的工資
select sal into newsal from emp where empno = eno;
dbms_output.put_line(‘漲後的工資:‘||newsal);
end;
-- 訪問只有輸入參數的存儲過程
call updateSal(7788,100);
如何創建存儲過程procedure
1、創建一個存儲過程用於保存已上架商品的數量
CREATE ORREPLACE PROCEDURE getGoodCount IS
goodCount int;
BEGIN
SELECT COUNT(*)INTO goodCount FROMtable_good where status = ‘3‘;
DBMS_OUTPUT.PUT_LINE(‘good表共有‘||goodCount||‘筆上架商品‘);
END getGoodCount;
call getGoodCount();
2、根據商品編號,查詢商品信息:
CREATE ORREPLACE PROCEDURE getgoodinfo(goodid IN NUMBER)IS
title table_good.good_title%TYPE;
BEGIN
SELECT good_titleINTO title FROMtable_good WHERE table_good.id=goodid;
DBMS_OUTPUT.PUT_LINE(goodid||‘號商品名稱為‘||title);
EXCEPTION
WHEN NO_DATA_FOUNDTHEN
DBMS_OUTPUT.PUT_LINE(‘沒有找到該商品‘);
END;
call getgoodinfo(2170);
3、創建有輸入和輸出參數的過程:
CREATE ORREPLACE PROCEDURE getgoodinforeturn(goodid IN NUMBER,v_re out VARCHAR2)IS
BEGIN
SELECT good_titleINTO v_re FROMtable_good WHERE table_good.id=goodid;
EXCEPTION
WHEN NO_DATA_FOUNDTHEN
DBMS_OUTPUT.PUT_LINE(‘沒有找到該商品‘);
END;
DECLARE
title VARCHAR2(100);
BEGIN
getgoodinforeturn(2170,title);
DBMS_OUTPUT.PUT_LINE(title);
END;
4、創建輸入輸出同類型參數的過程:
CREATE ORREPLACE PROCEDURE getgoodinforeturn2(d IN OUT NUMBER) IS
BEGIN
SELECT table_good.goods_salesINTO d FROMtable_good WHERE table_good.id=d;
EXCEPTION
WHEN NO_DATA_FOUNDTHEN
DBMS_OUTPUT.PUT_LINE(‘沒有找到該商品‘);
END;
DECLARE
sales Number(10);
BEGIN
sales:=4003;
getgoodinforeturn2(sales);
DBMS_OUTPUT.PUT_LINE(sales);
END;
5、默認值的過程 CREATE ORREPLACE PROCEDURE addGood
(
id NUMBER,
title VARCHAR2,
content VARCHAR2 :=‘CLERK‘,
mgr NUMBER,
hdate DATE DEFAULT SYSDATE,
sal NUMBER DEFAULT1000,
comm NUMBER DEFAULT0,
deptNo NUMBER DEFAULT30
)
AS
BEGIN
INSERT INTOtable_good VALUES(id,title,content,mgr,hdate,sal,comm,deptNo);
END;
EXEC addEmp(7776,‘zhangsan‘,‘CODER‘,7788,‘06-1月-2000‘,2000,0,10); --沒有使用默認值
EXEC addEmp(7777,‘lisi‘,‘CODER‘,7788,‘06-1月-2000‘,2000,NULL,10); --可以使用NULL值
EXEC addEmp(7778,‘wangwu‘,mgr=>7788); --使用默認值
EXEC addEmp(mgr=>7788,empNo=>7779,eName=>‘sunliu‘); --更改參數順序
...... ...... 還可以update,delete等等
二、常用命令
1、刪除存儲過程 DROP PROCEDURE Proc_Name; 2、查看過程狀態 SELECT object_name,status FROM USER_OBJECTS WHERE object_type=‘PROCEDURE‘; 3、重新編譯過程 ALTER PROCEDURE Proc_Name COMPILE; 4、查看過程代碼 SELECT * FROM USER_SOURCE WHERE TYPE=‘PROCEDURE‘;
三、關於循環:
1、loop declare
v_count number(2) := 0;
begin
loop
-- 循環開始
v_count := v_count + 1;
dbms_output.put_line(v_count);
exit whenv_count = 10; --當v_count等於10 時退出循環。
end loop; -- 循環結束
dbms_output.put_line(‘game over‘);
end;
2、while declare
v_count number(2) := 0;
begin
while v_count < 10 loop
-- 當v_count 小於10 執行循環
v_count := v_count + 1;
dbms_output.put_line(v_count);
end loop;
dbms_output.put_line(‘game over‘);
end;
3、for declare
v_count number(2) := 0; -- 此值對for 循環執行的次數沒有影響
begin
for v_count in 1 .. 10 loop
-- 此v_count 變量不是上面聲明的變量,循環10次
dbms_output.put_line(v_count);
end loop;
for v_count in reverse 1 .. 10 loop
--反序輸出
dbms_output.put_line(v_count);
end loop;
dbms_output.put_line(‘game over‘);
end;
4、goto declare
v_count number(2) := 0;
begin
for v_count in 1 .. 10 loop
dbms_output.put_line(v_count);
end loop;
for v_count in reverse 1 .. 10 loop
dbms_output.put_line(v_count);
if v_count = 5 then
goto endofloop;-- 跳至循環體外標簽處執行,循環結束
end if;
end loop;
<>
dbms_output.put_line(‘game over‘);-- 此處必須要有語句可以執行,若沒有也要寫 ‘null;‘
end;
四、關於異常 Exception
預定義異常:
declare
v_id t_12580_o2o_good.id%type := &id;
v_sales t_12580_o2o_good.goods_sales%type;
begin
select goods_sales into v_sales from t_12580_o2o_good where id = v_id;
dbms_output.put_line(‘the sales is :‘ || v_sales);
exception
when no_data_found then
dbms_output.put_line(‘no data found!‘);
when too_many_rows then
dbms_output.put_line(‘to many rows!‘);
when others then
dbms_output.put_line(sqlcode || ‘,‘ || sqlerrm);
end;
非預定義異常 01declare
02v_id t_12580_o2o_good.id%type := &id;
03no_result exception;
04begin
05update t_12580_o2o_goodset goods_sales = 1 where id = v_id;
06if sql%notfound then
07raise no_result;
08end if;
09exception
10when no_resultthen
11dbms_output.put_line(‘no data be update‘);
12when others then
13dbms_output.put_line(sqlcode || ‘-----‘|| sqlerrm);
14end;
五、關於遊標:
--顯式遊標: 01declare
02v_id table_good.id%type;
03v_sales table_good.goods_sales%type;
04cursor c_cursoris
05select id, goods_salesfrom table_good whereid between 2000 and 3000;
06begin
07open c_cursor;-- 打開遊標
08fetch c_cursor
09into v_id, v_sales;--獲取數據
10while c_cursor%found loop
11-- 當遊標裏有數據就執行下面的打印操作
12dbms_output.put_line(v_id || ‘ sales is : ‘|| v_sales);
13fetch c_cursor
14into v_id, v_sales;
15end loop;
16close c_cursor;
17end;
------------------------------------------------------------------------ 01declare
02-- 記錄類型變量,在遊標中存放所有列的數據。
03o2o_record_type table_good%rowtype;
04cursor v_cursor(v_sales table_good.goods_sales%type)is select * from table_good where goods_sales > v_sales;
05begin
06if v_cursor%isopen then
07fetch v_cursorinto o2o_record_type;
08else openv_cursor(1000); -- 若沒有打開,就先打開,再取數據
09fetch v_cursorinto o2o_record_type;
10end if;
11while v_cursor%found loop
12dbms_output.put_line(o2o_record_type.id ||‘ sales is: ‘ ||
13o2o_record_type.goods_sales);
14fetch v_cursor
15into o2o_record_type;
16end loop;
17dbms_output.put_line(v_cursor%rowcount); -- 遊標裏的數據的行數
18close v_cursor;
19end;
--隱式遊標 1declare
2v_deptno emp.deptno%type := &p_deptno;begin
3delete fromemp where deptno = v_deptno; -- 刪除 emp 表中對應部門號下的員工信息
4if sql%notfound then -- 如果對應部門沒有員工,則刪除 dept 表中對應的部門號,
5delete fromdept where deptno = v_deptno;
6commit;
7end if;
8rollback; -- 如果對應部門下有員工,則回滾至刪除前
9end;
--給銷量低於100的商品增加銷售基數100 01declare
02v_id table_good.id%type;
03v_sal table_good.goods_sales%type;
04v_sal_base table_good.goods_sales_base%type;
05cursor c_cursoris
06select id, goods_salesfrom table_good whereid between 1000 and 2000;
07begin
08open c_cursor;
09loop
10fetch c_cursor
11into v_id, v_sal;
12exit whenc_cursor%notfound;
13if v_sal <= 100 then
14v_sal_base := 100;
15update table_good
16set goods_sales_base = v_sal_base
17where id = v_id;
18dbms_output.put_line(v_id || ‘‘‘s goods_sales_base has been update! the new goods_sales_base is: ‘|| v_sal_base);
19end if;
20end loop;
21dbms_output.put_line(c_cursor%rowcount);
22close c_cursor;
23end;
-- FOR 循環操作遊標: view sourceprint? 01declare
02cursor c_cursoris
03select id,good_title,goods_salesfrom table_good whereid between 2000 and 3000;
04begin
05for v_recordin c_cursor loop
06-- 隱式地打開遊標,取數據
07if v_record.goods_sales <= 1200 then
08update table_goodset goods_sales_base = 100where id = v_record.id;
09dbms_output.put_line(v_record.good_title ||‘‘‘s sales_base has update!‘);
10end if;
11-- 隱式地關閉遊標
12end loop;
13end;
14-- 帶參數的遊標:
15declare
16cursor c_cursor(v_status varchar2default ‘3‘)is
17select id, goods_sales, good_title
18from table_good
19where status = v_statusand id between2000 and 3000;
20begin
21for c_rec in c_cursor(30) loop
22dbms_output.put_line(c_rec.id || ‘,‘|| c_rec.good_title || ‘,‘||
23c_rec.goods_sales);
24end loop;
25for c_rec in c_cursor loop
26-- 此處將會用默認值 20;
27dbms_output.put_line(c_rec.id || ‘,‘|| c_rec.good_title || ‘,‘||
28c_rec.goods_sales);
29end loop;
30end
存儲過程 有用