Oracle学习笔记之存储过程
存储过程
在Oracle中,可以在数据库中定义子程序,这种程序块称为存储过程(procedure)。它存放在数据字典中,可以在不同用户和应用程序之间共享,并可实现程序的优化和重用。使用存储过程的优点是:
(1) 过程在服务器端运行,执行速度快。
(2) 过程执行一次后代码就驻留在高速缓冲存储器,在以后的操作中,只需从高速缓冲存储器中调用已编译代码执行,提高了系统性能。
(3) 确保数据库的安全。可以不授权用户直接访问应用程序中的一些表,而是授权用户执行访问这些表的过程。非表的授权用户除非通过过程,否则就不能访问这些表。
(4) 自动完成需要预先执行的任务。过程可以在系统启动时自动执行,而不必在系统启动后再进行手工操作,大大方便了用户的使用,可以自动完成一些需要预先执行的任务。
注意:在用户的定义中不能使用下列对象创建语句:
CREATE VIEW、CREATE DEFAULT 、CREATE RULE 、CREATE PROCEDURE 、CREATE TRIGGER
SQL命令创建存储过程:
语法格式:
CREATE [OR REPLACE] PROCEDURE [schema.]procedure_name 如下通过实例描述存储过程结构和写法:
例如:[A1]
写一个存储过程:
例如:该存储过程根据人以部门编号,查询该部门中员工工资以及员工编号,如果如果员工工资低于该部门平均工资,此时将该员工工资涨薪为平均工资;
例如:上面题加一个条件,张薪资以后,将涨薪资的人员信息以游标形式返回;
例如:写一个存储过程将emp中empno和ename查询结果以out类型游标返回;
包中声明游标:
createorreplacepackage t is
type record_type isrecord(empno number,ename varchar2(20));
type cursor_type isrefcursorreturn record_type;
end t;
存储过程代码:
createorreplaceprocedure myProcedure(cur_out out t.cursor_type)is
begin
open cur_out forselect empno,ename from emp;
end myProcedure;
测试代码:
-- Created on 2017/10/19 by ADMINISTRATOR
declare
type cur_type isrefcursor;-- 弱类型游标
cur cur_type;
type record_type isrecord(empno number,ename varchar2(20));
employee record_type;
begin
myprocedure(cur);
loop
fetch cur into employee;
dbms_output.put_line(employee.ename||':'||employee.empno);
exitwhen cur%notfound;
endloop;
end;
实例2:
方法:
create or replace function login(username in varchar2,pwd in varchar2) return number is
res number(5);
flag number(1):=0;
begin
select count(*) into res from emp where ename=username and empno=pwd;
if res>0 then
flag:=1;
end if;
return flag;
end login;
存储过程:
create or replace procedure EmpProcedure(username in varchar2,pwd in varchar2,controlFlag in varchar2,
cur_emp out pac.cur_type1,cur_dpet out pac.cur_type2
) is
flag number(1);
begin
flag:=login(username,pwd);--验证用户是否登陆成功
if flag=1 --登陆成功
then
--判断controlFlag 1 查看该员工信息 2 查看emp表数据 3 查看该员工部门信息
case controlFlag
when 1 then --查看该员工信息
open cur_emp for
select * from emp where empno=pwd;
when 2 then
open cur_emp for
select * from emp;
when 3 then
open cur_dpet for
select * from dept where deptno=(select deptno from emp where emp.empno=pwd);
end case;
else--登陆失败
dbms_output.put_line('登陆失败');
end if;
end EmpProcedure;
包:
type r is record(empno number(5),ename varchar2(20),job varchar2(20),mgr number(5),hiredate date,sal number(15,2),comm number(15,2),deptno number(5));
type cur_type1 is ref cursor return r;
type p is record(deptno number(5),dname varchar2(20),loc varchar2(20));
type cur_type2 is ref cursor return p;
测试:
-- Created on 2017/10/21 by ADMINISTRATOR
declare
type r is record(empno number(5),ename varchar2(20),job varchar2(20),mgr number(5),hiredate date,sal number(15,2),comm number(15,2),deptno number(5));
type cur_type1 is ref cursor ;--注意不能声明为return 强制类型
type p is record(deptno number(5),dname varchar2(20),loc varchar2(20));
type cur_type2 is ref cursor;
cur1 cur_type1;
cur2 cur_type2;
res_1 r;
res_2 p;
flag number(1):=2;
begin
empprocedure('SMITH','123',flag,cur1,cur2);
if flag=1 or flag=2 then
loop
fetch cur1 into res_1;
exit when cur1%notfound;
dbms_output.put_line(res_1.empno||res_1.ename||res_1.hiredate);
end loop;
elsif flag=3 then
loop
fetch cur2 into res_2;
exit when cur2%notfound;
dbms_output.put_line(res_2.deptno||res_2.dname||res_2.loc);
end loop;
end if;
Exception when others then dbms_output.put_line('系统异常');
end;
305上课实例:
包:
create or replace package empdeptPac is
type cur_empType is ref cursor return emp%rowtype;
type cur_deptType is ref cursor return dept%rowtype;
end empdeptPac;
函数:
create or replace function emplogin(username in varchar2,pwd in varchar2) return boolean is
total number(10);
flag boolean :=false;
begin
select count(*) into total from emp where ename =username and empno=pwd;
if total>0 then
flag:=true;
else flag:=false;
end if;
return flag;
end emplogin;
存储过程:
create or replace procedure empdeptpro(username in varchar2,pwd in varchar2,flag in varchar2,
empitems out empdeptpac.cur_empType, deptitems out empdeptpac.cur_deptType, empres out emp%rowtype
) is
f boolean ;
begin
f:=emplogin(username,pwd);
if f then
case flag
when 'A' then--查询所有员工信息
open empitems for select * from emp;
when 'B' then--查询所在部门信息
open deptitems for select dept.* from emp,dept where dept.deptno=emp.deptno and emp.empno=pwd;
when 'C' then--查询个人信息
select emp.* into empres from emp where emp.empno=pwd;
end case;
end if;
end empdeptpro;
测试:
-- Created on 2017/11/4 by ADMINISTRATOR
declare
type curtype_dept is ref cursor;
type curtype_emp is ref cursor ;
cur_dept curtype_dept;
cur_emp curtype_emp;
empRow emp%rowtype;
begin
empdeptpro('SMITH',7369,'A',cur_emp,cur_dept,empRow);
loop
fetch cur_emp into empRow;
dbms_output.put_line(empRow.empno);
exit when cur_emp%notfound;
end loop;
end;
① IN:表示参数是输入给过程的;
② OUT:表示参数在过程中将被赋值,可以传给过程体的外部;
③ IN OUT:表示该类型的参数既可以向过程体传值,也可以在过程体中赋值。
Oracle学习笔记之存储过程的更多相关文章
- Oracle学习笔记三 SQL命令
SQL简介 SQL 支持下列类别的命令: 1.数据定义语言(DDL) 2.数据操纵语言(DML) 3.事务控制语言(TCL) 4.数据控制语言(DCL)
- Oracle学习笔记—数据字典和常用命令(转载)
转载自: oracle常用数据字典和SQL语句总结 Oracle常用命令大全(很有用,做笔记) 一.Oracle数据字典 数据字典是Oracle存放有关数据库信息的地方,其用途是用来描述数据的.比如一 ...
- oracle学习笔记第一天
oracle学习笔记第一天 --oracle学习的第一天 --一.几个基础的关键字 1.select select (挑选) 挑选出显示的--列--(可以多列,用“,”隔开,*表示所有列),为一条 ...
- Oracle学习笔记——点滴汇总
Oracle学习笔记——点滴汇总 http://www.botangdb.com/ Oracle GI = Grid Infrastructure = ASM + Cluster
- Oracle学习笔记之四sp1,Oracle 11g的常用函数
从Oracle学习笔记之四,SQL语言入门中摘出来的,独立成一章节 3.1 字符类函数 ASCII(c)和CHR(i) 分别用于返回一个字符的ASCII码和返回给定ASCII值所对应的字符. C ...
- Oracle学习笔记之四,SQL语言入门
1. SQL语言概述 1.1 SQL语言特点 集合性,SQL可以的高层的数据结构上进行工作,工作时不是单条地处理记录,而对数据进行成组的处理. 统一性,操作任务主要包括:查询数据:插入.修改和删除数据 ...
- oracle学习笔记(一)用户管理
--oracle学习第一天 --连接 @后面连接数据库实例,具体连接到那个数据库 conn scott/tiger@MYORA1; --修改密码 passw; --显示用户 show user; -- ...
- 吴裕雄--天生自然 oracle学习笔记:oracle理论学习详解及各种简单操作例子
1. 数据库的发展过程 层次模型 -->网状模型 -->关系模型 -->对象关系模型 2. 关于数据库的概念 DB:数据库(存储信息的仓库) DBMS:数据库管理系统(用于管理数据库 ...
- Oracle 学习笔记 18 -- 存储函数和存储过程(PL/SQL子程序)
PL/SQL子程序 它包含了函数和过程.此功能是指用户定义的函数.和系统功能是不同的.子程序通常完成特定的功能PL/SQL座.,能够被不同的应用程序多次调用.Oracle提供能够把PL/SQL程序存储 ...
随机推荐
- 201521123109《java程序设计》第四周学习总结
1. 本周学习总结 #1.1 尝试使用思维导图总结有关继承的知识点. #1.2 使用常规方法总结其他上课内容. - 了解了有关类的继承的知识 - 了解继承和多态的关系以及一些关键字内容 - 学习了O ...
- 201521123050《Java程序设计》第3周学习总结
1. 本周学习总结 2. 书面作业 1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; p ...
- Geronimo tomcat: 在 Apache Geronimo 插件体系中将 Apache Tomcat 这个优秀的 Web 容器整合至其中
Apache Geronimo 灵活的插件体系将 Tomcat, OpenJPA, OpenEJB, ActiveMQ 等第三方组件集成至其中.本文从多角度介绍了在 Apache Geronimo 中 ...
- svn清理失败且乱码 问题解决
由于昨天在网络不好的状态下频繁尝试svn更新,导致今天svn更新时出现:清理失败且乱码的情况如下: 以下是解决方案:1.下载sqlite3.exe ,地址为:http://download.csdn. ...
- Sping IOC
这2天学习了Spring的AOP 其中包括注解式和非注解式的配置 个人感觉注解式的配置非常好用.具体内容如下: 1. AOP 面向切面编程 个人理解就是在一个写好的方法上增加一些新的功能 ...
- 【Spring源码深度解析系列 】Spring整体架构
一.Spring的整体架构和模块 二.模块分类: 1.Core Container Core Container包含有Core .Beans.Context.和Expression Language ...
- ArrayListd的长度问题
namespace ArrayListd的长度问题{ class Program { static void Main(string[] args) { //需要的参数是object类型 //alt+ ...
- E - 今年暑假不AC HDU - 2037
"今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" "@#$%^&* ...
- Redis介绍——Linux环境Redis安装全过程和遇到的问题及解决方案
一:redis的入门介绍: 首先贴出官网; 英文:https://redis.io/ 中文:http://www.redis.cn/ 1.是什么 --REmote DIctionary Server( ...
- stdafx.h 的作用
stdafx.h VC工程里面经常见到stdafx.h这个头文件,以前也没有特别注意,但是这个文件用不好经常会出错. stdafx的英文全称为:Standard Application Framewo ...