Oracle系列之存储过程
涉及到表的处理请参看原表结构与数据 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
goto here;
end if;
if mod(x,i)=0 then
flag:=0;
goto here;
end if;
end loop;
<<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 table mytest(
name varchar2(30),
password varchar2(30)
);
create or replace procedure fj_pro1 is
begin
insert into mytest values('jack','');--执行部分
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;
case v_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 table users(
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;--自增
end loop;
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<=20 loop
insert into users values(v_num,fjname);
v_num:=v_num+1;
end loop;
end;
/
call fj_pro7('root1');--调用
select * from users;
分页:
create table book(
bookid number,bookname varchar2(50),publishhouse varchar2(50)
);
--in:表示这是一个输入参数,默认是in
create or replace procedure fj_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 procedure fj_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;
end testpackage;
/
建立存储过程
create or replace procedure fj_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 * from tb_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 procedure fenye(
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 into myrows;
--计算 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 procedure ex_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系列之存储过程的更多相关文章
- .Net程序员学用Oracle系列(7):视图、函数、存储过程、包
1.视图 1.1.创建.删除及调用普通视图 1.2.高级视图介绍 2.函数 2.1.系统函数介绍 2.2.创建.删除及调用自定义函数 3.存储过程 3.1.创建.修改及删除存储过程 3.2.调用存储过 ...
- .Net程序员学用Oracle系列(6):表、字段、注释、约束、索引
<.Net程序员学用Oracle系列:导航目录> 本文大纲 1.表 1.1.创建表 1.2.修改表 & 删除表 2.字段 2.1.添加字段 2.2.修改字段 & 删除字段 ...
- .Net程序员学用Oracle系列(7):视图、函数、过程、包
<.Net程序员学用Oracle系列:导航目录> 本文大纲 1.视图 1.1.创建视图 2.函数 2.1.创建函数 2.2.调用函数 3.过程 3.1.创建过程 3.2.调用过程 4.包 ...
- .Net程序员学用Oracle系列(25):触发器详解
1.触发器理论 1.1.触发器的应用场景 1.2.触发器的类型 1.3.DML 触发器的触发顺序 2.触发器实战 2.1.创建触发器 2.1.1.创建 DML 触发器 2.1.2.创建 DDL 触发器 ...
- .Net程序员学用Oracle系列(28):PLSQL 之SQL分类和动态SQL
1.SQL 语句分类 1.1.分类方法及类型 1.2.数据定义语言 1.3.数据操纵语言 1.4.其它语句 2.动态 SQL 理论 2.1.动态 SQL 的用途 2.2.动态 SQL 的语法 2.3. ...
- .Net程序员学用Oracle系列(30):零碎补充、最后总结(The End)
1.同义词 2.Flashback 技术 3.连接字符串的写法 4.转义字符 & 特殊运算符 5.文件类型 6.查看参数 & 修改参数 7.AWR 工具 8.学习方法 & 学习 ...
- .Net程序员学用Oracle系列(8):触发器、作业、序列、连接
1.触发器 2.作业 2.1.作业调度功能和应用 2.2.通过 DBMS_JOB 来调度作业 3.序列 3.1.创建序列 3.2.使用序列 & 删除序列 4.连接 4.1.创建连接 4.2.使 ...
- 【转】Oracle系列导航目录
.Net程序员学用Oracle系列(1):导航目录 .Net程序员学用Oracle系列(2):准备测试环境 .Net程序员学用Oracle系列(3):数据库编程规范 .Net程序员学用Oracle系列 ...
- 系列文章----.Net程序员学用Oracle系列
.Net程序员学用Oracle系列(18):PLSQL Developer 攻略 .Net程序员学用Oracle系列(17):数据库管理工具(SQL Plus) .Net程序员学用Oracle系列(1 ...
随机推荐
- 第二十七篇、使用MVVM布局页面
思路:架构的设计模式主要有这么两种 >MVC :这种方式用得很多,也很是常见,不在过多的介绍 >MVVM:使用这种 常常需要导入第三方框架,常见的是响应式框架 >主要讲一下ViewM ...
- C# 预处理指令
导读 1.什么是预处理 2.预处理的好处 3.C#中的常见预处理指令 4.总结 什么是预处理 在计算机科学中,预处理通常是指利用某一程序(通常是预处理器)对某一格式的源码(如.cs C ...
- (转)Quartz.NET管理类
最近做项目设计到Quartz.NET,写了一个Quartz.NET管理类,在此记录下. public class QuartzManager<T> where T : class,IJob ...
- js个人笔记
一.删除元素 <!DOCTYPE html> <html> <head> <title>删除元素</title> </head> ...
- Java基础从数组到集合之间关键字的区别!!!!
1.&& 和 &区别和联系: 相同点 : 结果是一样的. 不同点 :如果使用双&号判断,如果说条件一为false,不会判断条件二,但是单&号会继续判 ...
- STL 常见容器
vector: 是一种在结尾处高效插入.删除的容器,本质上是一个动态数组,可以自动维护数组的空间分配.它也允许在开头和中间插入.删除数据,但是效率极低. <span style="fo ...
- WPF中实现根据拼音查找汉字
1.WPF的一个触摸屏项目,需要输入姓名,但是屏幕不支持汉字输入,使用虚拟键盘不稳定,为了解决该问题特此进行处理. 2.新建一个类转换类,里面初始化一个数组,数组包含拼音,以及拼音下的常用的汉字. 3 ...
- NpoiUtil
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- python通过webservice连接cmdbuild
cmdbuild的部署可以查看文章:http://20988902.blog.51cto.com/805922/1541289 部署成功后,访问http://192.168.1.1:8080/cmdb ...
- python 常用模块(转载)
转载地址:http://codeweblog.com/python-%e5%b8%b8%e7%94%a8%e6%a8%a1%e5%9d%97/ adodb:我们领导推荐的数据库连接组件bsddb3:B ...