Oracle(四):PL/SQL、存储函数、存储过程、触发器
一、PL/SQL
(一)什么是 PL/SQL
[declare--声明变量
]
begin--代码逻辑
[exception--异常处理
]
end;
(二)变量
--变量的用法--
declarev_price number(10,2);--水费单价v_usenum number; --水费字数v_usenum2 number(10,2);--吨数v_money number(10,2);--金额
beginv_price:=2.45;--水费单价v_usenum:=8012;--字数--字数换算为吨数v_usenum2:= round( v_usenum/1000,2);--计算金额v_money:=round(v_price*v_usenum2,2);dbms_output.put_line('单价:'||v_price||'吨 数:'||v_usenum2||'金额:'||v_money);
end;
declarev_price number(10,2);--单价v_usenum number;--水费字数v_num0 number;--上月字数v_num1 number;--本月字数v_usenum2 number(10,2);--使用吨数v_money number(10,2);--水费金额
begin--对单价进行赋值v_price:=3.45;--变量赋值select usenum,num0,num1 into v_usenum,V_num0,V_num1 from
T_ACCOUNT where year='2012' and month='01' and owneruuid=1;v_usenum2:= round(v_usenum/1000,2);v_money:=v_price*v_usenum2;DBMS_OUTPUT.put_line('单价:'||v_price||'吨数:'||v_usenum2||'金额:'||v_money||'上月字数:'||v_num0||'本月
字数'||v_num1);
end;
(三)属性类型
declarev_price number(10,2);--单价v_usenum T_ACCOUNT.USENUM%TYPE;--水费字数v_num0 T_ACCOUNT.NUM0%TYPE;--上月字数v_num1 T_ACCOUNT.NUM1%TYPE;--本月字数v_usenum2 number(10,2);--使用吨数v_money number(10,2);--水费金额
begin--对单价进行赋值v_price:=3.45;--v_usenum:=8090;select usenum,num0,num1 into v_usenum,V_num0,V_num1 from
T_ACCOUNT where year='2012' and month='01' and owneruuid=1;--使用吨数v_usenum2:= round(v_usenum/1000,2);--计算金额v_money:=v_price*v_usenum2;DBMS_OUTPUT.put_line('单价:'||v_price||'吨数:'||v_usenum2||'金额:'||v_money||'上月字数:'||v_num0||'本月
字数'||v_num1);
end;
--变量的用法--
declarev_price number(10,2);--单价v_account T_ACCOUNT%ROWTYPE;--记录型v_usenum2 number(10,2);--使用吨数v_money number(10,2);--水费金额
begin--对单价进行赋值v_price:=3.45;--赋值select * into v_account from T_ACCOUNT where year='2012' and month='01' and owneruuid=1;--使用吨数v_usenum2:= round(v_account.usenum/1000,2);--计算金额v_money:=v_price*v_usenum2;DBMS_OUTPUT.put_line('单价:'||v_price||'吨数:'||v_usenum2||'金额:'||v_money||'上月字数:
'||v_account.num0||'本月字数'||v_account.num1);
end;
(四)异常
exception
when 异常类型 then
异常处理逻辑
--变量的用法--
declarev_price number(10,2);--水费单价v_usenum T_ACCOUNT.USENUM%type; --水费字数v_usenum2 number(10,3);--吨数v_money number(10,2);--金额
beginv_price:=2.45;--水费单价select usenum into v_usenum from T_ACCOUNT where
owneruuid=1 and year='2012' and month='01';--字数换算为吨数v_usenum2:= round( v_usenum/1000,3);--计算金额v_money:=round(v_price*v_usenum2,2);dbms_output.put_line('单价:'||v_price||'吨 数:'||v_usenum2||'金额:'||v_money);
exceptionwhen NO_DATA_FOUND thendbms_output.put_line('未找到数据,请核实');when TOO_MANY_ROWS thendbms_output.put_line('查询条件有误,返回多条信息,请核实');
end;
(五)条件判断
if 条件 then业务逻辑
end if;
基本语法 2
if 条件 then业务逻辑
else业务逻辑
end if;
基本语法 3
if 条件 then业务逻辑
elsif 条件 then业务逻辑
else业务逻辑
end if;
需求:设置三个等级的水费 5 吨以下 2.45 元/吨 5 吨到 10 吨部分 3.45 元/吨 , 超过 10 吨部分 4.45 ,根据使用水费的量来计算阶梯水费。
declarev_price1 number(10,2);--不足 5 吨的单价v_price2 number(10,2);--超过 5 吨不足 10 吨单价v_price3 number(10,2);--超过 10 吨单价v_account T_ACCOUNT%ROWTYPE;--记录型v_usenum2 number(10,2);--使用吨数v_money number(10,2);--水费金额
begin--对单价进行赋值v_price1:=2.45;v_price2:=3.45;v_price3:=4.45;--赋值select * into v_account from T_ACCOUNT where year='2012' and month='01' and owneruuid=1;--使用吨数v_usenum2:= round(v_account.usenum/1000,2);--计算金额(阶梯水费)if v_usenum2<=5 then--第一个阶梯v_money:=v_price1*v_usenum2;elsif v_usenum2>5 and v_usenum2<=10 then --第二个阶梯v_money:=v_price1*5 + v_price2*( v_usenum2-5); else --第三个阶梯v_money:=v_price1*5 +v_price2*5 +
v_price3*( v_usenum2-10 );end if;DBMS_OUTPUT.put_line('吨数:'||v_usenum2||'金额:'||v_money||'上月字数: '||v_account.num0||'本月字数'||v_account.num1);
exceptionwhen NO_DATA_FOUND thenDBMS_OUTPUT.put_line('没有找到数据');when TOO_MANY_ROWS thenDBMS_OUTPUT.put_line('返回的数据有多行');
end;
(六)循环
loop--循环语句
end loop;
declare
v_num number:=1;
begin loopdbms_output.put_line(v_num);v_num:=v_num+1;exit when v_num>100;end loop;
end ;
while 条件
loop
end loop;
declare
v_num number:=1;
beginwhile v_num<=100loopdbms_output.put_line(v_num);v_num:=v_num+1; end loop;
end ;
for 变量 in 起始值..终止值
loopend loop;
beginfor v_num in 1..100loopdbms_output.put_line(v_num); end loop;
end;
(七)游标
1.什么是游标
2.语法结构及示例
cursor 游标名称 is SQL 语句
使用游标语法
open 游标名称
loopfetch 游标名称 into 变量 exit when 游标名称%notfound
end loop;
close 游标名称
declarev_pricetable T_PRICETABLE%rowtype;--价格行对象cursor cur_pricetable is select * from T_PRICETABLE where
ownertypeid=1;--定义游标
beginopen cur_pricetable;--打开游标loopfetch cur_pricetable into v_pricetable;--提取游标到变量exit when cur_pricetable%notfound;--当游标到最后一行下面退出循环dbms_output.put_line( '价格:'||v_pricetable.price ||'吨位:'||v_pricetable.minnum||'-'||v_pricetable.maxnum ); end loop; close cur_pricetable;--关闭游标
end ;
declarev_pricetable T_PRICETABLE%rowtype;--价格行对象cursor cur_pricetable(v_ownertypeid number) is select *
from T_PRICETABLE where ownertypeid=v_ownertypeid;--定义游
标
beginopen cur_pricetable(2);--打开游标loop fetch cur_pricetable into v_pricetable;--提取游标到变量exit when cur_pricetable%notfound;--当游标到最后一行下面退出循环dbms_output.put_line('价格:'||v_pricetable.price ||'吨位:'||v_pricetable.minnum||'- '||v_pricetable.maxnum ); end loop; close cur_pricetable;--关闭游标
end ;
4. for 循环提取游标值
declarecursor cur_pricetable(v_ownertypeid number) is select *
from T_PRICETABLE where ownertypeid=v_ownertypeid;--定义游
标
beginfor v_pricetable in cur_pricetable(3)
loop dbms_output.put_line('价格:'||v_pricetable.price ||'吨
位:'||v_pricetable.minnum||'-'||v_pricetable.maxnum ); end loop;
end ;
二、存储函数
(一)什么是存储函数
(二)存储函数语法结构
CREATE [ OR REPLACE ] FUNCTION 函数名称
(参数名称 参数类型, 参数名称 参数类型, ...)
RETURN 结果变量数据类型
IS变量声明部分;
BEGIN逻辑部分;RETURN 结果变量;
[EXCEPTION 异常处理部分]
END;
(三)案例
create function fn_getaddress(v_id number)
return varchar2
isv_name varchar2(30);
beginselect name into v_name from t_address where id=v_id;return v_name;
end;
select id 编号,name 业主名称,fn_getaddress(addressid) 地址
from t_owners
查询结果:
三、存储过程
(一)什么是存储过程
(二)存储过程语法结构
CREATE [ OR REPLACE ] PROCEDURE 存储过程名称
(参数名 类型, 参数名 类型, 参数名 类型)
IS|AS变量声明部分;
BEGIN逻辑部分
[EXCEPTION 异常处理部分]
END;
(三)案例
--增加业主信息序列
create sequence seq_owners start with 11;
--增加业主信息存储过程
create or replace procedure pro_owners_add
(v_name varchar2,v_addressid number,v_housenumber varchar2, v_watermeter varchar2,v_type number
)
isbegininsert into T_OWNERS
values( seq_owners.nextval,v_name,v_addressid,v_housenumb
er,v_watermeter,sysdate,v_type );commit;
end;
call pro_owners_add('赵伟',1,'999-3','132-7',1);
/**
* 增加
* @param owners
*/
public static void add(Owners owners){java.sql.Connection conn=null;java.sql.CallableStatement stmt=null;try {conn=BaseDao.getConnection();stmt=conn.prepareCall("{call
pro_owners_add(?,?,?,?,?)}");stmt.setString(1, owners.getName());stmt.setlong(2, owners.getAddressid());stmt.setString(3, owners.getHousenumber());stmt.setString(4, owners.getWatermeter());stmt.setlong(5, owners.getOwnertypeid());stmt.execute();}catch (SQLException e) {e.printStackTrace();}finally {BaseDao.closeAll(null, stmt, conn);}return id;
}
四、触发器
(一)什么是触发器
- 数据确认
- 实施复杂的安全性检查
- 做审计,跟踪表上所做的数据操作等
- 数据的备份和同步
- 前置触发器(BEFORE)
- 后置触发器(AFTER)
(二)创建触发器的语法
语法:
CREATE [or REPLACE] TRIGGER 触发器名
BEFORE | AFTER
[DELETE ][[or] INSERT] [[or]UPDATE [OF 列名]]
ON 表名
[FOR EACH ROW ][WHEN(条件) ]
declare
……
begin
PLSQL 块
End ;
FOR EACH ROW 作用是标注此触发器是行级触发器 语句级触发器
(三)案例
create or replace trigger tri_account_updatenum1
before
update of num1
on t_account
for each row
declare
begin
:new.usenum:=:new.num1-:new.num0;
end;
2. 后置触发器
--创建业主名称修改日志表:用于记录业主更改前后的名称
create table t_owners_log
(
updatetime date,
ownerid number,
oldname varchar2(30),
newname varchar2(30)
);
--创建后置触发器,自动记录业主更改前后日志
create trigger tri_owners_log
after
update of name
on t_owners
for each row
declarebegininsert into t_owners_log
values(sysdate,:old.id,:old.name,:new.name);
end;
--更新数据
update t_owners set name='杨小花' where id=3;
commit;
--查询日志表
select * from t_owners_log;
五、综合案例
minnum 为下限值 ,maxnum 为上限值。上边的记录的含义是
- 超过 5 吨不足 10 吨的价格为 3.45
- 超过 10 吨以上的价格为 4.45
- 如果吨数为 12。计算如下:
考虑到阶梯的层次可能是不确定的,所以我们需要通过游标查询出阶梯价格记 录,然后计算每一阶梯的水费,然后相加。伪代码如下:
金额=0
循环价格表{if( 上限值为空 或者 总吨数<上限值 ) -- 最高阶梯
{//此为最后阶梯 ,数量为超过上限值部分的吨数金额=金额+ 价格*(总吨数- 上限值)退出循环
}
else
{//此为非最后阶梯 ,数量为区间内的吨数金额=金额+ 价格*(上限值- 下限值)
} }
declarev_ownertypeid number;--业主类型 IDv_usenum2 number(10,2);--总吨数v_money number(10,2);--总金额cursor cur_pricetable(v_type number) is select * from
t_pricetable where ownertypeid=v_type;--价格游标v_pricetable t_pricetable%rowtype;--每阶梯价格对象beginv_ownertypeid:=1;v_usenum2:=12;v_money:=0;for v_pricetable in cur_pricetable(v_ownertypeid)loopif v_pricetable.maxnum is null or
v_usenum2<=v_pricetable.maxnum then--最后阶梯 (总吨数-下限值)*价格v_money:=v_money+
v_pricetable.price*(v_usenum2-v_pricetable.minnum);exit;else--非最后阶梯 (上限值-下限值)* 价格v_money:=v_money+
v_pricetable.price*(v_pricetable.maxnum-v_pricetable.minn
um);end if; end loop;DBMS_OUTPUT.put_line('阶梯水费金额:'||v_money);
end;
create or replace function fn_calmoney(v_ownertypeid
number,v_usenum2 number)
return number
isv_pricetable t_pricetable%rowtype;--价格行对象v_money number(10,2);--金额cursor cur_pricetable(v_type number) is select * from
t_pricetable where ownertypeid=v_type order by minnum;--定
义游标
beginv_money:=0;--金额for v_pricetable in cur_pricetable(v_ownertypeid)loop --计算阶梯水费--如果水费小于最大值,或最大值为 null 表示此阶梯为最后一个阶梯,--价格*(总吨数-此阶梯下限值)if v_usenum2<= v_pricetable.maxnum or
v_pricetable.maxnum is null thenv_money:=v_money+ v_pricetable.price* ( v_usenum2 -
v_pricetable.minnum); exit; else -- 价格*(此阶梯上限值-此阶梯下限值)v_money:=v_money+ v_pricetable.price*
(v_pricetable.maxnum-v_pricetable.minnum ); end if;end loop; return v_money;
end;
select fn_calmoney(1,12) from dual;
create or replace trigger tri_account_updatenum1
before
update of num1
on t_account
for each row
declarev_usenum2 number(10,2);--吨数
begin--使用数赋值:new.usenum:=:new.num1-:new.num0; v_usenum2:= round( :new.usenum/1000,3);--计算吨数:new.money:=fn_calmoney(:new.ownertype,v_usenum2);--对金
额列赋值
end ;
- 1. 如何取得年和月 用 to_char()函数
- 2. 如何取得区域 ID 参数中没有直接提供区域 ID,我们可以通过 addressid到 address 表查询
create or replace procedure pro_owners_add
(v_name varchar2,v_addressid number,v_housenumber varchar2,v_watermeter varchar2,v_type number,v_ownersuuid out number
)
isv_area number;--区域编号v_year char(4);--年份v_month char(2);--月份
begin--提取序列值到变量select seq_owners.nextval into v_ownersuuid from dual;--根据地址编号查询区域编号select areaid into v_area from t_address where
id=v_addressid;--年份v_year:=to_char(sysdate ,'yyyy');--月份v_month:=to_char(sysdate,'mm');--增加业主信息insert into t_owners
values( v_ownersuuid,v_name,v_addressid,v_housenumber,v_w
atermeter,sysdate,v_type );--增加账务表信息insert into t_account
(id,owneruuid,ownertype,areaid,year,month,num0) values
(seq_account.nextval,v_ownersuuid,v_type,v_area,v_year,
v_month,0 );commit;
exceptionwhen NO_DATA_FOUND thenv_ownersuuid:=-1;rollback;
end;
Oracle(四):PL/SQL、存储函数、存储过程、触发器相关推荐
- Oracle在Pl/sql中的存储过程----zhoudianzhang
Oracle 存储过程 目录 Oracle 存储过程... 1 Oracle存储过程基础知识... 1 Oracle存储过程的基本语法... 2 关于Oracle存储过程的若干问题备忘... 4 1. ...
- 从永远到永远-Oracle的PL/SQL学习(存储过程学习)
工作原因,涉及到了Oracle数据库的存储过程.以前没有接触过,请教了很多大佬,被喷的.被误导.被折腾的半4.总算是领悟了一点点的东西,现有时间总结下自己的一些感触. 1.关于PL/SQL 1.1 是 ...
- Oracle数据库PL/SQL中执行存储过程
有时生产线上问题,或者调试需要手动在pl/sql中执行存储过程,但一般不建议这么做(影响线上交易). 一.直接测试执行 在pl/sql中找到对应需要测试的存储过程,左边菜单栏:Procedures下. ...
- Oracle使用PL/SQL自定义函数
这里写目录标题 一.PL/SQL概述 二.变量和常量 2.1变量 2.2赋值方式 2.3常量 2.4常量和变量的区别 三,%type和%rowtype 3.1 %type 3.2 %rowtype 四 ...
- oracle 查看函数被哪些触发器引用_oracle如何查看存储过程,存储函数,触发器的具体内容...
(1)set serveroutput on 实现plsql developer 打印输出 (2)如何查看存储过程,存储函数,触发器的内容 查 user_sources表 eg:查询GET_DEPT_ ...
- 使用wrap和unwrap加密解密Oracle的PL/SQL对象(包,存储过程,函数等)代码
使用wrap和unwrap加密解密Oracle的PL/SQL对象(包,存储过程,函数等)代码 Oracle数据库系统自带的PL/SQL对象(包,存储过程,函数等)的代码绝大部分都是使用了wrap程序加 ...
- oracle if函数变量,Oracle数据库——PL/SQL编程
PL/SQL块基本结构 declare -- 声明部分 begin -- 执行部分 exception -- 异常处理部分 end; 声明部分:包含变量.常量定义,由 declare 关键字开始,如果 ...
- 使用oracle 的 PL/Sql 定时执行一个存储过程
/* 使用oracle 的 PL/Sql 定时执行一个存储过程 测试目的:在表 tab 中间隔一分钟插入一条数据 实现方案: 1.通过 oracle 中 dbms_job 完成存储过程的定时调用 2. ...
- 【MySQL数据库设计与应用(六)】存储程序(存储过程,存储函数,触发器,事件)
文章目录 1 存储程序介绍 1.1 什么是存储程序 1.2 存储例程 1.3 触发器 1.4 事件 2 创建和调用存储过程 2.1 创建和调用存储过程 2.2 存储过程的参数模式 2.3 存储过程返回 ...
- pl/sql操作数据库之触发器的使用
pl/sql操作数据库之触发器的使用 这篇文章讲述的是pl/sql操作数据库之触发器的使用,如有错误或不当之处,还望各位大神批评指正. 什么是触发器? 触发器是许多关系数据库系统都提供的一项技术.在O ...
最新文章
- Windows下Hadoop的环境安装[转]
- Knockout应用开发指南(完整版) 目录索引
- PAT:1090. Highest Price in Supply Chain (25) AC
- mysql在linux下的安装_mysql在linux下的安装
- 第三方登录android代码,Android Learning:微信第三方登录(示例代码)
- 计算机二级access选择题技巧,计算机二级access考试注意事项及解题技巧策略
- 你见过哪些堪称绝妙的数学证明?
- 创业者没有周末,但有周期
- [译] JAVA初学者的30个常见问题
- Google 重返中国的忧虑
- [转载]UML时序图总结
- 测试“测试”的“测试”
- gstreamer实现sink插件的代码,及无法结束的问题
- 软件测试理论-测试用例和设计方法
- C语言,最新猴子摘桃(递归方法)
- 德州大学奥斯汀分校计算机专业排名,德州大学奥斯汀分校专业排名一览及最强专业推荐(USNEWS美国大学排名)...
- 共模干扰与差模干扰(及其消除)
- espnet中的transformer和LSTM语言模型对比实验
- java课程设计 成绩_Java课程设计—学生成绩管理系统(201521123005 杨雪莹)
- 网络安全岗位介绍——等级保护测评师