Oracle系列之存储过程

摘要:
涉及到表的处理请参看原表结构与数据Oracle建表插数据等等判断是否是素数:createorreplaceprocedureisPrime(xnumber)asflagnumber:=1;beginifxxthengotohere;endif;ifmod(x,i)=0the

涉及到表的处理请参看原表结构与数据Oracle建表插数据等等

判断是否是素数:
create or replace procedure isPrime(x number) as
flag number:=1;
begin
if x<2 then
dbms_output.put_line('not prime');
else
for i in 2..x 
loop
if i*i>x then
gotohere;
end if;
if mod(x,i)=0 then
flag:=0;
gotohere;
end if;
endloop;
<<here>>
if flag=1 then
dbms_output.put_line('is prime');
else
dbms_output.put_line('not prime');
end if;
end if;
end;
/

--调用

call isPrime(10);
call isPrime(11);
创建一个存储过程,该过程可以向某表中添加记录
create tablemytest(
name varchar2(30),
password varchar2(30)
);
create or replace  procedure fj_pro1 is
begin
insert into mytest values('jack','123456');--执行部分
end;
/
--执行过程删除名称为jack对应的列
create or replace  procedure fj_pro1 is
begin
delete from mytest where name='jack';--执行部分
end;
/

replace表示如果有相同的procedurename就替换,fj_pro1表示procedurename
查看错误信息:
show error;
如何调用该过程

call procedurename(参数值1,参数值2);
创建过程,根据雇员名修改工资
create or replace procedure fj_pro2(a1name varchar2,a1sal number) is
begin
update tb_Employee set sal=a1sal where ename=a1name; 
end;
/
call fj_pro2('SCOTT',150);--这样SCOTT的工资就被修改成了150
select * from tb_Employee where ename='SCOTT';
编写一个过程,输入雇员名,工资低于2000的雇员工资增加10%
create or replace procedure fj_pro3(fjname varchar2) is
v_sal tb_Employee.sal%type;--定义
begin
select sal into v_sal from tb_Employee where ename=fjname;--执行
if v_sal<2000 then--判断
update tb_Employee set sal=sal*1.1 where ename=fjname;
end if;
end;
/
call fj_pro3('SCOTT');--调用
select * from tb_Employee where ename='SCOTT';
雇员如果补助为零则加200,如果不为零则加100
create or replace procedure fj_pro4(fjname varchar2) is
v_comm tb_Employee.comm%type;--定义
begin
select comm into v_comm from tb_Employee where ename=fjname;--执行
if v_comm<>0 then--判断
update tb_Employee set comm=comm+100 where ename=fjname;
else
update tb_Employee set comm=comm+200 where ename=fjname;
end if;
end;
/
call fj_pro4('SCOTT');--调用
select * from tb_Employee where ename='SCOTT';
三个条件分支 if-then-elsif-else

输入雇员雇员号,如果该雇员的职位是PRESIDENT就给他的工资增加1000,如果该雇员的职位是MANAGER就给他的工资增加500,其它职位的雇员工资增加200。

create or replace procedure fj_pro5(fjno number) is
v_job tb_Employee.job%type;--定义
begin
select job into v_job from tb_Employee where pk_Employee_ID=fjno;--执行
if v_job='PRESIDENT' then
update tb_Employee set sal=sal+1000 where pk_Employee_ID=fjno;
elsif v_job='MANAGER' then
update tb_Employee set sal=sal+500 where pk_Employee_ID=fjno;
else
update tb_Employee set sal=sal+200 where pk_Employee_ID=fjno;
end if;
end;
/
call fj_pro5(7788);--调用
select * from tb_Employee where ename='SCOTT';
以员工号为参数,修改该员工的工资,10号部门加150,20号加200,30号加250,其他加300
create or replace procedure updatesal(p_empno tb_Employee.pk_Employee_ID%type) as
v_deptno tb_Employee.deptno%type;
v_inc tb_Employee.sal%type;
begin
select deptno into v_deptno from tb_Employee where pk_Employee_ID=p_empno;  
casev_deptno
when 10 then v_inc:=150;
when 20 then v_inc:=200;
when 30 then v_inc:=250;
else  v_inc:=300;
end case;
update tb_Employee set sal=sal+v_inc where pk_Employee_ID=p_empno;   
end;
/
call updatesal(7788);--调用
select * from tb_Employee where ename='SCOTT';
循环语句 loop
users表中,用户雇员号从1开始增加
create tableusers(
userno number,
db_user varchar2(40)
);
create or replace procedure fj_pro6(fjname varchar2) is
v_num number:=1;--定义:=表示赋值
begin
loop
insert into users values(v_num,fjname);
exit when v_num=10;--判断退出条件
v_num:=v_num+1;--自增
endloop;
end;
/
call fj_pro6('root1');--调用,这样表中会有10个db_user为root1的列
select * from users;

继续加上10个root1

create or replace procedure fj_pro7(fjname varchar2) is
v_num number:=11;--定义:=表示赋值
begin
while v_num<=20loop
insert into users values(v_num,fjname);
v_num:=v_num+1;
endloop;
end;
/
call fj_pro7('root1');--调用
select * from users;
分页:
create tablebook(
bookid number,bookname varchar2(50),publishhouse varchar2(50)
);
--in:表示这是一个输入参数,默认是in
create or replace procedurefj_pro8(
fjbookid in number,
fjbookname in varchar2,
fjpublishhouse in varchar2
) is
begin 
insert into book values(fjbookid,fjbookname,fjpublishhouse);
end;
/

在java中调用

OraclPro2.java

create or replace procedurefj_pro9(
fjno in number,
fjname out varchar2,
fjsal out number,
fjjob out varchar2
) is
begin 
select ename,sal,job into fjname,fjsal,fjjob from tb_Employee where pk_Employee_ID=fjno;
end;
/

OraclPro3.java
返回结果集
创建一个包

create or replace package testpackage as
type test_cursor is ref cursor;
endtestpackage;
/

建立存储过程

create or replace procedurefj_pro10(
fjno in number,
p_sursor out testpackage.test_cursor
) is
begin
open p_sursor for select * from tb_Employee where deptno=fjno;
end;
/

OraclPro4.java
分页

select t1.*,rownum rn from (select * fromtb_Employee) t1;
select t1.*,rownum rn from (select * from tb_Employee) t1 where rownum<=5;
select * from (select t1.*,rownum rn from(select * from tb_Employee) t1 where rownum<=5) where rn>=2;

开始编写分页的过程

create or replace procedurefenye(
table_name in varchar2,
pagesize in number,--每页大小
pagenow in number,--当前页
myrows out number,--总记录数
mypagecount out number,--总页数
p_sursor out testpackage.test_cursor--返回的记录集
) is
v_sql varchar2(1000);--定义部分,定义一个sql语句
v_begin number:=(pagenow-1)*pagesize+1;
v_end number:=pagenow*pagesize;
begin
v_sql:='select * from (select t1.*,rownum rn from(select * from '||table_name||'order by sal) t1 where rownum<='||v_end||') where rn>='||v_begin;--执行部分
open p_sursor for v_sql;--把游标和sql语句关联起来
--计算 myrows和 mypagecount
v_sql:='select COUNT(*) from '||table_name;
--执行sql,并把返回的值,赋给 myrows
execute immediate v_sql intomyrows;
--计算 mypagecount
if mod(myrows,pagesize)=0 then
mypagecount:=myrows/pagesize;
else
mypagecount:=myrows/pagesize+1;
end if;
close p_sursor;--关闭游标
end;
/

使用java测试
OraclePro5.java
例外处理

declare
v_ename tb_Employee.ename%type;--定义
begin
--
select ename  into v_ename from tb_Employee where pk_Employee_ID=&no;
dbms_output.put_line('雇员名是:'||v_ename);
exception 
when no_data_found then
dbms_output.put_line('雇员号输入有误');
end;
/
处理预定义例外
case_not_found
create or replace procedure fj_pro11(fjno number) is
v_sal tb_Employee.sal%type;
begin
select sal into v_sal from tb_Employee where pk_Employee_ID=fjno;
case
when v_sal<1000 then
update tb_Employee set sal=sal+100 where pk_Employee_ID=fjno;
when v_sal<2000 then
update tb_Employee set sal=sal+200 where pk_Employee_ID=fjno;
end case;
exception
when case_not_found then
dbms_output.put_line('case语句没有与'||v_sal||'相匹配的条件');
end;
/
call fj_pro11(7900);
create or replace procedureex_test(
fjno number
) is
--定义一个例外
myex exception;
begin 
update tb_Employee set sal=sal+1000 where pk_Employee_ID=fjno;--更新用户sal
--sql%notfound这是表示没有 update
--raise myex;触发 myex
if sql%notfound then
raise myex;
end if;
exception
when myex then
dbms_output.put_line('没有更新任何用户');
end;
/
call ex_test(7788);--调用
call ex_test(1111);--没定义例外之前这样也是不会出错的

免责声明:文章转载自《Oracle系列之存储过程》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇基于无锁的C#并发队列实现Git、Github和GitLab的区别及与SVN的比较下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Vue项目中左右布局支持拉伸宽度

<template> <el-row :gutter="10"> <el-col :span="5" v-show="type === '2' && sidebar.opened" > <data-tree :t...

八:SQL之DQL数据查询语言单表操作

前言:   DQL数据库查询语言是我们在开发中最常使用的SQL,这一章总结了单表操作部分的常用查询方式   主要操作有:查询所有字段、查询指定字段、查询指定记录、带IN的关键字查询,范围查询,陪查询、查询空值   带AND的多条件查询、带OR的多条件查询,关键字DISTINCT,查询结果排序,分组查询。分页查询等 准备好数据测试表  1 mysql&g...

【总结】数据库优化的若干方法

导图 下图是我结合自己的经验以及搜集整理的数据库优化相关内容的思维导图,如果图片不清楚,可以在浏览器中右键,在新窗口中查看(Chrome)或者查看图像(FireFox)。 常用关键字优化 在编写T-SQL的时候,会使用很多功能类似的关键字,比如COUNT和EXISTS、IN和BETWEEN AND等,我们往往会根据需求直奔主题地来编写查询脚本,完成需求要...

layui select动态添加option

<form class="layui-form" action=""> <div class="layui-form-item proSelect"> <label class="layui-form-label">产品类别</label> <div...

Oracle Express Edition

Oracle Express Edition(简洁版/开发版)非注册下载安装及SQL*Plus的简单使用 https://www.cnblogs.com/cjw1115/p/6209439.html 使用工具的第一步就是安装工具,配置环境!下面就Oracle 11g Express的安装和简单实用做一简介。 一.下载安装过程 去oracle的官网下载Ora...

SQL获取当天0点和23点59分方法

--获取当天的00:00:00 SELECT CONVERT(DATETIME,CONVERT(VARCHAR(10),GETDATE(),120)) SELECT convert(varchar(10),getdate(),120)--是将当前时间的类型转换成字符类型,并只取出年月日信息。 SELECT convert(varchar(10),getda...