服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Oracle - Oracle存储过程案例详解

Oracle存储过程案例详解

2021-09-26 17:18wx41768626 Oracle

这篇文章主要介绍了Oracle存储过程案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

创建简单存储过程(Hello World)

为了方便读者简单易懂,我将下面使用到的表复制给大家。
具体表中的数据,请大家自己填写

  1. -- Create table
  2. create table EMP
  3. (
  4. empno NUMBER(4) not null,
  5. ename VARCHAR2(10),
  6. job VARCHAR2(9),
  7. mgr NUMBER(4),
  8. hiredate DATE,
  9. sal NUMBER(7,2),
  10. comm NUMBER(7,2),
  11. deptno NUMBER(2)
  12. )

Oracle存储过程案例详解

Oracle存储过程案例详解

  1. create or replace procedure firstP(name in varchar2) is
  2. /*这里name为的参数,in为输入,varchar2为类型*/
  3. begin
  4. /* dbms_output.put_line(); 相当输出到控制台上,这样我们一个简单的存储过程就完成啦
  5. 记住一句话的结束使用分号结束,存储过程写完一定要执行
  6. 将它保存到数据库中 (F8)快捷键,或者点击左上角执行*/
  7. dbms_output.put_line('我的名字叫'||name);/*dbms_output.put_line相当于JAVA中的System.out.println("我的名字叫"+name);*/
  8. end firstP;

下面我们要对刚刚写过的存储过程进行测试,我们开启Test Window这个窗口

Oracle存储过程案例详解

  1. -- Created on 2018/12/30 星期日 by ADMINISTRATOR
  2. declare
  3. -- Local variables here
  4. /*测试名称 名称类型 使用 := 给参数赋值,在多说一句,分号结束本句*/
  5. name2 varchar2(64):='数据库';
  6. begin
  7. -- Test statements here
  8. firstp(name2);
  9. end;

我们打开DBMS Output就可以看到执行的存储过程啦。

Oracle存储过程案例详解

Oracle存储过程案例详解

存储过程IF判断

  1. create or replace procedure isifp(age in number) is
  2. /*存储过程if判断以then开始,以end if; 结束*/
  3. begin
  4. if (age > 30) then
  5. dbms_output.put_line('我已经超过30岁了');
  6. else
  7. if (age < 10) then
  8. dbms_output.put_line('我还是个儿童');
  9. else
  10. dbms_output.put_line('我正在奋斗时期');
  11. end if;
  12. end if;
  13.  
  14. end;

存储过程输出

  1. create or replace procedure inandout(name in varchar2, age in number,outp out varchar2) is
  2. /*in 代表输入,out 代表输出*/
  3. begin
  4. outp:='my name is '|| name ||',my age is '||age;/*相当于JAVA中的return outp,但是请注意,存储过程中可以return多个值*/
  5. end inandout;

测试输出代码

  1. -- Created on 2018/12/30 星期日 by ADMINISTRATOR
  2. declare
  3. -- Local variables here
  4. name varchar2(64):='数据库';
  5. age number:=06;
  6. out_p varchar2(64);
  7. begin
  8. -- Test statements here
  9. inandout(name,age,outp=>:out_p);
  10. /*这里的outp是存储过程中的输出参数,out_p是在测试中使用的别名*/
  11. end;

Oracle存储过程案例详解

返回游标

  1. create or replace procedure sysrefcursor(id in number, columnss out sys_refcursor) as
  2. /*columnss out sys_refcursor 为输出游标*/
  3. begin
  4. open columnss for
  5. select * from emp where empno=id;
  6. end;

测试游标

第一种测试方法

  1. -- Created on 2018/12/30 星期日 by ADMINISTRATOR
  2. declare
  3. -- Local variables here
  4. cursor ee is select * from emp where empno=7934;
  5. begin
  6. -- Test statements here
  7. for e in ee loop
  8. dbms_output.put_line('deptno:'||e.deptno);
  9. end loop;
  10. end;

输出结果如下:

Oracle存储过程案例详解

第二种测试方法

  1. -- Created on 2018/12/30 星期日 by ADMINISTRATOR
  2. declare
  3. -- Local variables here
  4. cursor ee is select * from emp where empno=7934;
  5. cur ee % rowtype;
  6. begin
  7. -- Test statements here
  8. open ee;
  9. loop
  10. fetch ee into cur;
  11. exit when ee%notfound;
  12. dbms_output.put_line('name:'||cur.ename);
  13. end loop;
  14. close ee;
  15. end;

Oracle存储过程案例详解

上面测试结果仅仅返回一条数据。下面我来演示返回多条数据的情况。
首先请看我表中的数据

Oracle存储过程案例详解

有两个job中内容为CLERK的数据。

  1. -- Created on 2018/12/30 星期日 by ADMINISTRATOR
  2. declare
  3. -- Local variables here
  4. cursor ee is select * from emp where job='CLERK';
  5. begin
  6. -- Test statements here
  7. for e in ee loop
  8. dbms_output.put_line('deptno:'||e.deptno);
  9. end loop;
  10. end;

Oracle存储过程案例详解

游标返回多条数据。

由于对于初学者来说,游标可能不是很容易理解,下面我用JAVA语言来描述一下。
我们在java程序中写条件查询的时候,返回出来的数据是List<泛型>。这个操作相当于游标,说白了就是个查询而已(大家不要误认为就这么一句简单的SQL为什么要用游标,因为只是方便读者学习游标罢了,具体业务具体分析,请不要抬杠哦)
当我们要使用list中的数据时,我们使用循环调用某一条数据时,是不是就要用实体类对象点get字段。可以理解为for e in ee loop dbms_output.put_line('deptno:'||e.deptno); end loop;
这里面的e.deptno。

获取table中的column

  1. create or replace procedure intop(id in number, print2 out varchar2) as
  2. e_name varchar2(64);
  3. begin
  4. select ename into e_name from emp where empno = id;
  5. if e_name ='ALLEN' then
  6. dbms_output.put_line(e_name);
  7. print2:='my name is '||e_name;
  8. else if e_name ='SMITH' then
  9. print2:='打印sql'||e_name;
  10. else
  11. print2:='打印其他';
  12. end if;
  13. end if;
  14. end intop;

稍微复杂一点存储过程

由于朋友这里有个需求需要用存储过程,进而更新一下博客。
首先我们先创建一张表

  1. -- Create table
  2. create table CLASSES
  3. (
  4. id NUMBER not null,
  5. name VARCHAR2(14),
  6. classesc VARCHAR2(10),
  7. seq NUMBER(5)
  8. )
  9. tablespace USERS
  10. pctfree 10
  11. initrans 1
  12. maxtrans 255
  13. storage
  14. (
  15. initial 64K
  16. next 1M
  17. minextents 1
  18. maxextents unlimited
  19. );
  20. -- Create/Recreate primary, unique and foreign key constraints
  21. alter table CLASSES
  22. add constraint PK_CLASSES primary key (ID)
  23. using index
  24. tablespace USERS
  25. pctfree 10
  26. initrans 2
  27. maxtrans 255
  28. storage
  29. (
  30. initial 64K
  31. next 1M
  32. minextents 1
  33. maxextents unlimited
  34. );

下面我们创建一个序列

  1. -- Create sequence
  2. create sequence SEQ_CLASSES
  3. minvalue 1
  4. maxvalue 9999999999999999999999999999
  5. start with 2
  6. increment by 1
  7. cache 20;

下面创建存储过程,写的乱一些,希望不要介意

  1. create or replace procedure proclasses(Names in varchar2,
  2. classescs in varchar) as
  3. /*在我们创建存储过程的时候as其实是is*/
  4. id number;/*设置变量名称*/
  5. c number;
  6. seq number;
  7. begin
  8. select SEQ_CLASSES.nextval into id from dual;/*获取下一个序列,使用into赋值给id这个变量名称*/
  9. dbms_output.put_line('classescs=' || classescs);/*打印而已*/
  10. select count(*) into c from Classes where classesc = classescs;/*条件判断,classesc=进来的变量*/
  11. if (c > 0) then/*当数量大于0时*/
  12. select max(seq) + 1 into seq from Classes where classesc = classescs;
  13. dbms_output.put_line('第一个seq' || seq);
  14. else
  15. if (c = 0) then
  16. seq := 0;/*如果查询出来的数量为0的时候,我们赋值seq变量为0*/
  17. dbms_output.put_line('c=0的时候seq' || seq);
  18. end if;
  19. end if;
  20. insert into classes
  21. (id, name, classesc, seq)
  22. values
  23. (id, names, classescs, seq);
  24. /*insert插入这个不用多说了,大家都明白;注意的是我们insert之后一定要提交。
  25. 不然数据没有持久化到数据库,这个insert没有任何意义了*/
  26. end proclasses;

下面我们来调用这个存储过程

  1. -- Created on 2019/1/7 星期一 by ADMINISTRATOR
  2. declare
  3. -- Local variables here
  4. names varchar2(32):='晓明';
  5. classescs varchar2(32):='一班';
  6. begin
  7. -- Test statements here
  8. proclasses(names,classescs);
  9. end;

到此这篇关于Oracle存储过程案例详解的文章就介绍到这了,更多相关Oracle存储过程内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_41768626/article/details/85406750

延伸 · 阅读

精彩推荐