Oracle入门基础(十二)一一储存过程及触发器
2024-10-19 22:48:50
1.第一个存储过程
打印Hello World
调用存储过程:
1.exec sayhelloworld();
2.begin
sayhelloworld();
sayhelloworld();
end;
create or replace procedure sayhelloworld
as
--说明部分
begin
dbms_output.put_line('Hello World');
end;
2.带参数的存储过程
给指定的员工涨100,并且打印涨前和涨后的薪水
create or replace procedure raiseSalary(eno in number)
is
--定义变量保存涨前的薪水
psal emp.sal%type;
begin
--得到涨前的薪水
select sal into psal from emp where empno=eno;
--涨100
update emp set sal=sal+100 where empno=eno;
--要不要commit?
dbms_output.put_line('涨前:'||psal||' 涨后:'||(psal+100));
end raiseSalary;
3.存储函数
查询某个员工的年收入
create or replace function queryEmpIncome(eno in number)
return number
is
--定义变量保存月薪和奖金
psal emp.sal%type;
pcomm emp.comm%type;
begin
--得到月薪和奖金
select sal,comm into psal,pcomm from emp where empno=eno;
--返回年收入
return psal*12+nvl(pcomm,0);
end queryEmpIncome;
4.out参数
–查询某个员工的姓名 薪水和职位
/*
1、查询某个员工的所有信息 —> out参数太多
2、查询某个部门中的所有员工信息 ----> 返回的是集合
*/
create or replace procedure queryEmpInformation(eno in number,
pename out varchar2,
psal out number,
pjob out varchar2)
is
begin
select ename,sal,job into pename,psal,pjob from emp where empno=eno;
end queryEmpInformation;
5.在out参数中使用光标
查询某个部门中的所有员工信息 ----> 返回的是集合
create or replace package mypackage is
type empcursor is ref cursor;
procedure queryEmpList(dno in number,empList out empcursor);
end mypackage;
create or replace package body mypackage is
procedure queryEmpList(dno in number,empList out empcursor)
as
begin
open empList for select * from emp where deptno=dno;
end;
end mypackage;
6.第一个触发器
每当成功插入新员工后,自动打印“成功插入新员工”
create trigger firsttrigger
after insert
on emp
declare
begin
dbms_output.put_line('成功插入新员工');
end;
触发器应用一
实施复杂的安全性检查
禁止在非工作时间 插入新员工
1、周末: to_char(sysdate,‘day’) in (‘星期六’,‘星期日’)
2、上班前 下班后:to_number(to_char(sysdate,‘hh24’)) not between 9 and 17
create or replace trigger securityemp
before insert
on emp
begin
if to_char(sysdate,'day') in ('星期六','星期日','星期五') or
to_number(to_char(sysdate,'hh24')) not between 9 and 17 then
--禁止insert
raise_application_error(-20001,'禁止在非工作时间插入新员工');
end if;
end securityemp;
触发器应用二
/*
数据的确认
涨后的薪水不能少于涨前的薪水
create or replace trigger checksalary
before update
on emp
for each row
begin
--if 涨后的薪水 < 涨前的薪水 then
if :new.sal < :old.sal then
raise_application_error(-20002,'涨后的薪水不能少于涨前的薪水。涨前:'||:old.sal||' 涨后:'||:new.sal);
end if;
end checksalary;
最新文章
- 模仿东京首页banner轮播,京东新闻上下滚动动画实现(动画实现)
- 掀起你的盖头来:Unit Of Work-工作单元
- Solr3.6.1 在Tomcat6下的环境搭建
- 【POJ 1390】Blocks
- docker-5 docker仓库
- PowerShell 导出SharePoint管理中心解决方式
- HDU-1565 方格取数(1)
- 通过 yum update 将系统从CentOS 6.2 升级到 CentOS 6.6 及升级过程中的简单排错
- jQuery的animate方法在IE8下出现小问题
- iphone开发教程下载
- 第6周-接口、内部类与Swing
- Sql Server中的nvarchar(n)和varchar(n)
- github上最好的开源MMORPG - stendhal
- js获取当前浏览器地址栏的链接,然后在链接后面加参数
- GC参考手册 —— GC 算法(基础篇)
- DB2还原数据库备份
- Laravel线上布暑到linux的问题汇总
- Oracle中把一张表查询结果插入到另一张表中
- [DEBUG]椭圆的中点Bresenham算法边缘绘制出现错误
- Simple Web API Server in Golang (1)
热门文章
- RFC2544学习频率“Learning Frequency”详解—信而泰网络测试仪实操
- 入手这个商业智能工具,建设自助式BI平台轻松搞定
- 在 CentOS 或 RHEL 系统上检查可用的安全更新的方法
- 【C# 线程】数据槽 LocalDataStoreSlot简称DataSlot
- 【C#基础概念】命名规范
- python-can库基于PCAN-USB使用方法
- JDK安装及JAVA_HOME配置
- 60天shell脚本计划-4/12-渐入佳境
- MongoDB数据库的下载安装及配置方法
- c/c++ 常见字符串处理函数总结 strlen/sizeof strcpy/memcpy/strncpy strcat/strncat strcmp/strncmp sprintf/sscanf strtok/split/getline atoi/atof/atol