标签:
一、如何创建存储过程procedure
1、创建一个存储过程用于保存已上架商品的数量
CREATEORREPLACEPROCEDUREgetGoodCountIS
goodCountint;
BEGIN
SELECTCOUNT(*)INTOgoodCountFROMtable_goodwherestatus =‘3‘;
DBMS_OUTPUT.PUT_LINE(‘good表共有‘||goodCount||‘笔上架商品‘);
ENDgetGoodCount;
call getGoodCount();2、根据商品编号,查询商品信息:
CREATEORREPLACEPROCEDUREgetgoodinfo(goodidINNUMBER)IS
title table_good.good_title%TYPE;
BEGIN
SELECTgood_titleINTOtitleFROMtable_goodWHEREtable_good.id=goodid;
DBMS_OUTPUT.PUT_LINE(goodid||‘号商品名称为‘||title);
EXCEPTION
WHENNO_DATA_FOUNDTHEN
DBMS_OUTPUT.PUT_LINE(‘没有找到该商品‘);
END;
call getgoodinfo(2170);3、创建有输入和输出参数的过程:
CREATEORREPLACEPROCEDUREgetgoodinforeturn(goodidINNUMBER,v_reoutVARCHAR2)IS
BEGIN
SELECTgood_titleINTOv_reFROMtable_goodWHEREtable_good.id=goodid;
EXCEPTION
WHENNO_DATA_FOUNDTHEN
DBMS_OUTPUT.PUT_LINE(‘没有找到该商品‘);
END;
DECLARE
title VARCHAR2(100);
BEGIN
getgoodinforeturn(2170,title);
DBMS_OUTPUT.PUT_LINE(title);
END;4、创建输入输出同类型参数的过程:
CREATEORREPLACEPROCEDUREgetgoodinforeturn2(dINOUTNUMBER)IS
BEGIN
SELECTtable_good.goods_salesINTOdFROMtable_goodWHEREtable_good.id=d;
EXCEPTION
WHENNO_DATA_FOUNDTHEN
DBMS_OUTPUT.PUT_LINE(‘没有找到该商品‘);
END;
DECLARE
sales Number(10);
BEGIN
sales:=4003;
getgoodinforeturn2(sales);
DBMS_OUTPUT.PUT_LINE(sales);
END;5、默认值的过程
CREATEORREPLACEPROCEDUREaddGood
(
id NUMBER,
title VARCHAR2,
content VARCHAR2 :=‘CLERK‘,
mgr NUMBER,
hdateDATEDEFAULTSYSDATE,
sal NUMBERDEFAULT1000,
comm NUMBERDEFAULT0,
deptNo NUMBERDEFAULT30
)
AS
BEGIN
INSERTINTOtable_goodVALUES(id,title,content,mgr,hdate,sal,comm,deptNo);
END;
EXECaddEmp(7776,‘zhangsan‘,‘CODER‘,7788,‘06-1月-2000‘,2000,0,10);--没有使用默认值
EXECaddEmp(7777,‘lisi‘,‘CODER‘,7788,‘06-1月-2000‘,2000,NULL,10);--可以使用NULL值
EXECaddEmp(7778,‘wangwu‘,mgr=>7788);--使用默认值
EXECaddEmp(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);
exitwhenv_count = 10;--当v_count等于10 时退出循环。
endloop;-- 循环结束
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);
endloop;
dbms_output.put_line(‘game over‘);
end;3、for
declare
v_count number(2) := 0;-- 此值对for 循环执行的次数没有影响
begin
forv_countin1 .. 10 loop
-- 此v_count 变量不是上面声明的变量,循环10次
dbms_output.put_line(v_count);
endloop;
forv_countinreverse 1 .. 10 loop
--反序输出
dbms_output.put_line(v_count);
endloop;
dbms_output.put_line(‘game over‘);
end;4、goto
declare
v_count number(2) := 0;
begin
forv_countin1 .. 10 loop
dbms_output.put_line(v_count);
endloop;
forv_countinreverse 1 .. 10 loop
dbms_output.put_line(v_count);
if v_count = 5then
gotoendofloop;-- 跳至循环体外标签处执行,循环结束
endif;
endloop;
<<endofloop>>
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
05updatet_12580_o2o_goodsetgoods_sales = 1whereid = v_id;
06if sql%notfoundthen
07raise no_result;
08endif;
09exception
10whenno_resultthen
11dbms_output.put_line(‘no data be update‘);
12whenothersthen
13dbms_output.put_line(sqlcode ||‘-----‘|| sqlerrm);
14end;五、关于游标:
--显式游标:
01declare
02v_id table_good.id%type;
03v_sales table_good.goods_sales%type;
04cursorc_cursoris
05selectid, goods_salesfromtable_goodwhereidbetween2000and3000;
06begin
07openc_cursor;-- 打开游标
08fetchc_cursor
09intov_id, v_sales;--获取数据
10while c_cursor%found loop
11-- 当游标里有数据就执行下面的打印操作
12dbms_output.put_line(v_id ||‘ sales is : ‘|| v_sales);
13fetchc_cursor
14intov_id, v_sales;
15endloop;
16closec_cursor;
17end;------------------------------------------------------------------------
01declare
02-- 记录类型变量,在游标中存放所有列的数据。
03o2o_record_type table_good%rowtype;
04cursorv_cursor(v_sales table_good.goods_sales%type)isselect*fromtable_goodwheregoods_sales > v_sales;
05begin
06if v_cursor%isopenthen
07fetchv_cursorintoo2o_record_type;
08elseopenv_cursor(1000);-- 若没有打开,就先打开,再取数据
09fetchv_cursorintoo2o_record_type;
10endif;
11while v_cursor%found loop
12dbms_output.put_line(o2o_record_type.id ||‘ sales is: ‘||
13o2o_record_type.goods_sales);
14fetchv_cursor
15intoo2o_record_type;
16endloop;
17dbms_output.put_line(v_cursor%rowcount);-- 游标里的数据的行数
18closev_cursor;
19end;--隐式游标
1declare
2v_deptno emp.deptno%type := &p_deptno;begin
3deletefromempwheredeptno = v_deptno;-- 删除 emp 表中对应部门号下的员工信息
4if sql%notfoundthen-- 如果对应部门没有员工,则删除 dept 表中对应的部门号,
5deletefromdeptwheredeptno = v_deptno;
6commit;
7endif;
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;
05cursorc_cursoris
06selectid, goods_salesfromtable_goodwhereidbetween1000and2000;
07begin
08openc_cursor;
09loop
10fetchc_cursor
11intov_id, v_sal;
12exitwhenc_cursor%notfound;
13if v_sal <= 100then
14v_sal_base := 100;
15updatetable_good
16setgoods_sales_base = v_sal_base
17whereid = v_id;
18dbms_output.put_line(v_id ||‘‘‘s goods_sales_base has been update! the new goods_sales_base is: ‘|| v_sal_base);
19endif;
20endloop;
21dbms_output.put_line(c_cursor%rowcount);
22closec_cursor;
23end;-- FOR 循环操作游标:
01declare
02cursorc_cursoris
03selectid,good_title,goods_salesfromtable_goodwhereidbetween2000and3000;
04begin
05forv_recordinc_cursor loop
06-- 隐式地打开游标,取数据
07if v_record.goods_sales <= 1200then
08updatetable_goodsetgoods_sales_base = 100whereid = v_record.id;
09dbms_output.put_line(v_record.good_title ||‘‘‘s sales_base has update!‘);
10endif;
11-- 隐式地关闭游标
12endloop;
13end;
14-- 带参数的游标:
15declare
16cursorc_cursor(v_status varchar2default‘3‘)is
17selectid, goods_sales, good_title
18fromtable_good
19wherestatus = v_statusandidbetween2000and3000;
20begin
21forc_recinc_cursor(30) loop
22dbms_output.put_line(c_rec.id ||‘,‘|| c_rec.good_title ||‘,‘||
23c_rec.goods_sales);
24endloop;
25forc_recinc_cursor loop
26-- 此处将会用默认值 20;
27dbms_output.put_line(c_rec.id ||‘,‘|| c_rec.good_title ||‘,‘||
28c_rec.goods_sales);
29endloop;
30end;
标签:
原文地址:http://blog.csdn.net/u014421556/article/details/51897176