oracle存储过程(返回列表的存储结合游标使用)总结 以及在java中的调用
这段时间开始学习写存储过程,主要原因还是因为工作需要吧,本来以为很简单的,但几经挫折,豪气消磨殆尽,但总算搞通了,为了避免后来者少走弯路,特记述与此,同时亦对自己进行鼓励。
以下是我在开发项目中第一次编写的存储,里面用到了嵌套游标和嵌套循环,以及变量的定义方式,第一次不规范的地方还请多多包含,有不明白的地方也可以给我留言,大家互相学习。
--准考证 随机生成 存储过程
--生成规则:在用户选择考试关联好考点和考场之后,点击自动生成准考证,准考证按照 当年考试次数后四位+岗位类型+考点编号+考场编号+座位号 这种规则随机生成
--备注:一个考场下面只能做三十个人,并且不同岗位类型的考生不能在同一考场,但是同一岗位类型的不同岗位可以在同一个考场
--注意1.变量命名的时候不能和字段名一样
create or replace procedure card_random_generate(exam_id in number,examNum in varchar,creater in varchar) as
--定义变量
exam_stu_num number;--考试下所有考生的数量
job_name exam_job.jobname%type;--岗位名称
job_code exam_job.jobcode%type;--岗位编号
job_type exam_job.jobtype%type;--岗位类型
job_stu_num number;--每个岗位下报考的考生数量
point_id exam_point.id%type;--考场对应考点的考点id
point_num exam_point.pointnum%type;--考点编号
point_name exam_point.pointname%type;--考点名称
room_num exam_point.pointnum%type;--考场编号
room_name exam_point.pointname%type;--考场名称
room_id ep_exam_point.id%type;--考场在关联表中对应的id
room_number number;--每个考试下考场的个数
studentid exam_student.id%type;--考生的id
seat_num exam_card.seatnum%type;--座位号
card_num exam_card.cardnum%type;--准考证号
i number;--循环变量
j number;--每个岗位类别需要的考场数
rn number;
--定义查询岗位游标 规则:1.按照岗位类型分组 2.统计每个类型处于缴费状态以及审核过的考生的数量 3.岗位状态是正常状态
CURSOR student_job IS
select j.jobtype,count( j.jobtype) as job_stu_num from exam_student e inner join exam_job j on e.examid=exam_id
and e.jobid=j.id and e.status='6' and e.ispay='1' and e.state='1' and j.status='1' group by j.jobtype;
--定义 根据考试id查询关联的考点 和考场信息(按照考点id升序)
CURSOR exam_point_room IS
select e.id,p.pointname, p.pointnum,p.parent,p.ptype,(select c.pointnum from exam_point c where c.id=p.parent) as point_number,e.isuser from ep_exam_point e
left join exam_point p on e.pointid=p.id or e.centernum=p.id where examid=exam_id and p.ptype='2' and p.state='1' and e.isuser='1' order by p.id;
begin
select count(*) into exam_stu_num from exam_student s where examid=exam_id and s.ispay='1' and s.status='6' and s.state='1';--所有已审核,已缴费成功,未生成准考证的考生数量
dbms_output.put_line('该考试下所有的考生数量是:'||exam_stu_num);
--一级循环标准 按岗位类型
for c in student_job loop
begin
job_type:=c.jobtype;--岗位类型
job_stu_num:=c.job_stu_num;--每个岗位类型下的考生数量
if(mod(job_stu_num,30)=0) then
j:=job_stu_num/30;
else
j:=floor(job_stu_num/30)+1;
end if;
dbms_output.put_line('岗位类型是:'||job_type||',报考岗位的考生数量是:'||job_stu_num||',岗位需要的考场数量是:'||j);
--二级循环 按照每个岗位下考生的数量
while job_stu_num>0
loop
begin
--三级循环标准 按照考场
for d in exam_point_room loop
-- if(d.isuser=2)then
-- exit;
-- end if;
begin
if(d.ptype=2) then
point_num:=d.point_number;
-- select id into point_id from exam_point where id=d.parent;--查询考场的父节点考点的考点id
point_id:=d.parent;--考场的父节点考点的考点id
room_num:=d.pointnum;
room_name:=d.pointname;
room_id:=d.id;
dbms_output.put_line('考点编号是:'|| point_num||',考场编号是:'||room_num);
i:=1;
while i<=30 --每个考场座位号都是从01-30
loop
begin --这里是取满足状态的学生 随机取一个把生成的准考证与学生id一一对应,在此之前一直非常无奈的是在oracle中要想查询排过序的特定行结果一般都要用到子查询,而在存储过程中
--给变量赋值时候在子查询里面赋值一直不能编译成功,几经尝试后来放在外面把子查询作为一个整体在外面实现了赋值功能,好坑啊,不过最终还是实现了,呵呵
select b.id into studentid from (select s.* ,rownum as rn from exam_student s inner join exam_job j on s.jobid=j.id and s.examid=j.examid
and s.status='6' and s.state='1' and s.ispay='1' where s.examid=exam_id and j.jobtype=job_type) b where rn=job_stu_num ;
dbms_output.put_line('考生id是:'||studentid);
if(i<10) then
dbms_output.put_line('准考证号是:'|| examNum||job_type||point_num||room_num||'0'||i||',考场名称是:'||room_name);
card_num:=examNum||job_type||point_num||room_num||'0'||i;--拼接准考证号
seat_num:='0'||i;
insert into exam_card values(seq_exam_card_id.nextval,exam_id,studentid,point_id,room_num,seat_num,card_num,creater,sysdate);
commit;
else
dbms_output.put_line('准考证号是:'|| examNum||job_type||point_num||room_num||i||',考场名称是:'||room_name);
card_num:=examNum||job_type||point_num||room_num||i;--拼接准考证号
seat_num:=i;
insert into exam_card values(seq_exam_card_id.nextval,exam_id,studentid,point_id,room_num,seat_num,card_num,creater,sysdate);
commit;
end if;
update exam_student set status='7' where id=studentid;--改变学生的状态为7 生成准考证状态
commit;
i:=i+1;
job_stu_num:=job_stu_num-1;
end;
if(job_stu_num<=0) then
update ep_exam_point set isuser='2' where id=room_id;
commit;
exit;--退出循环
end if;
end loop;
end if;
end;
if(job_stu_num=0)then
exit;--退出循环
end if;
if(job_stu_num>0) then
update ep_exam_point set isuser='2' where id=room_id;
commit;
exit;--退出循环(此时一个考场排满了,退出循环进入下一个考场继续排)
end if;
end loop;
end;
end loop;
end;
end loop;
end card_random_generate;
oracle存储过程(返回列表的存储结合游标使用)总结 以及在java中的调用的更多相关文章
- Oracle创建表语句(Create table)语法详解及示例、、 C# 调用Oracle 存储过程返回数据集 实例
Oracle创建表语句(Create table)语法详解及示例 2010-06-28 13:59:13| 分类: Oracle PL/SQL|字号 订阅 创建表(Create table)语法详解 ...
- oracle 存储过程 返回结果集
oracle 存储过程 返回结果集 CreationTime--2018年8月14日09点50分 Author:Marydon 1.情景展示 oracle存储过程如何返回结果集 2.解决方案 最简 ...
- 123 c#调用oracle存储过程返回数据集 --通过oracle存储过程返回数据集
c#调用oracle存储过程返回数据集 2008-12-20 10:59:57| 分类: net|字号 订阅 CREATE OR REPLACE PACKAGE pkg_tableTypeIS ...
- oracle 存储过程返回结果集 (转载)
好久没上来了, 难道今天工作时间稍有空闲, 研究了一下oracle存储过程返回结果集. 配合oracle临时表, 使用存储过程来返回结果集的数据读取方式可以解决海量数据表与其他表的连接问题. 在存储过 ...
- oracle 存储过程返回结果集
好久没上来了, 难道今天工作时间稍有空闲, 研究了一下oracle存储过程返回结果集. 配合oracle临时表, 使用存储过程来返回结果集的数据读取方式可以解决海量数据表与其他表的连接问题. 在存储过 ...
- c#调用oracle存储过程返回数据集
c#调用oracle存储过程返回数据集 2008-12-20 10:59:57| 分类: net|字号 订阅 CREATE OR REPLACE PACKAGE pkg_tableTypeIS ...
- 160307、Java调用Oracle存储过程返回结果集
一:无返回值的存储过程调用 存储过程: CREATE OR REPLACE PROCEDURE PRO_1(PARA1 IN VARCHAR2,PARA2 IN VARCHAR2) AS BEGI ...
- oracle存储过程返回数据集结果
MSSQL的存储过程返回数据集好简单,直接SELECT 就可以. ORACLE的存储过程返回数据集必须通过游标. 创建ORACLE存储过程 create or replace procedure cx ...
- java调用oracle存储过程返回多条结果集
oracle版本:11g oracle存储过程,使用游标的方式返回多行.多列数据集合: CREATE OR REPLACE PROCEDURE SP_DATA_TEST( /*P_ID IN INT, ...
随机推荐
- 网络代理-Firefox在shadow socks下面的使用
好久不写了,嘿嘿,中午好哈大家,给大家介绍下firefox下配置shadowsocks使用代理. 第一步:先下载一个firefox. 第二步: 打开设置 找到组件选项. 3.第三步: 4.第四步: 5 ...
- SQL 语句快速参考
来自 W3CSchool 的 SQL 快速参考 SQL 语句 语法 AND / OR SELECT column_name(s)FROM table_nameWHERE conditionAND|OR ...
- boost::lexical_cast
boost::lexical_cast为数值之间的转换(conversion)提供了一揽子方案,比如:将一个字符串"转换成整数123,代码如下: "; int a = lexica ...
- [Android] 开源框架 xUtils HttpUtils 代理设置 (Temporary Redirect错误)
今天简单学习了一下xUtils的使用 https://github.com/wyouflf/xUtils 其中用到HttpUtils模块时,发现总是出现Temporary Redirect 错误. 查 ...
- 【BZOJ4071】[Apio2015]巴邻旁之桥 Treap
[BZOJ4071][Apio2015]巴邻旁之桥 Description 一条东西走向的穆西河将巴邻旁市一分为二,分割成了区域 A 和区域 B. 每一块区域沿着河岸都建了恰好 1000000001 ...
- vue ios自带拼音全键输入法模糊查询兼容性问题
ios的自带拼音全键会在输入框中输入拼音,直接在输入框用@keyup="autoInput()"的话,在监听输入事件的时候安卓显示正常, ios就会出现输入显示数据不灵敏 解决办法 ...
- 阿里云OSS分片上传DEMO
分片传输规则 1.不能超过10000片,2.每片必须大于100KB using System; using System.Collections.Generic; using System.Compo ...
- Ubuntu 12.04安装Google Chrome(转)
下载google chrome deb包,下载地址:https://www.google.com/chrome/browser/desktop/index.html,google的网站被墙了,如果你下 ...
- Python 最难的问题
Python 最难的问题 超过十年以上,没有比解释器全局锁(GIL)让Python新手和专家更有挫折感或者更有好奇心. 未解决的问题 随处都是问题.难度大.耗时多肯定是其中一个问题.仅仅是尝试解决这个 ...
- (3.14) set statistics io/time/profile /SET SHOWPLAN_ALL ON详解统计信息与执行计划
SQL Server读懂语句运行的统计信息 SET STATISTICS TIME IO PROFILE ON 执行计划详细描述请参考(读懂执行计划) 对于语句的运行,除了执行计划本身,还有一些其他 ...