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程序存储 ...
随机推荐
- 201521123108《Java程序设计》第14周学习总结
1. 本周学习总结 2. 书面作业 Q1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自己的学号.姓名) 答: 在自己建立的数据库上执行常见SQL语句(截 ...
- 用Beautifulsoup 来爬取贴吧图片
import urllib.request import bs4 import re import os url="https://tieba.baidu.com/p/1988291937? ...
- POJ 3625 最小生成树 Prim C++
Building Roads Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11861 Accepted: 3376 D ...
- Activiti第一篇【介绍、配置开发环境、快速入门】
Activiti介绍 什么是Activiti? Activiti5是由Alfresco软件在2010年5月17日发布的业务流程管理(BPM)框架,它是覆盖了业务流程管理.工作流.服务协作等领域的一个开 ...
- FastDFS安装全过程记录(V5.05)
FastDFS安装全过程记录 1.安装准备 HA虚拟IP:192.168.1.208 HA软件:Keepalived 操作系统:CentOS 7 用户:root 数据目录:/data/fastdfs ...
- nmap扫描某段网络连通性
nmap -v -sP 10.0.10.0/24 进行ping扫描,打印出对扫描做出响应的主机,不做进一步测试(如端口扫描或者操作系统探测): nmap -sP 192.168.1.0/24 仅列出指 ...
- 《MATLAB从入门到放弃》二维曲线和图形绘制基础(一): 什么是图形对象和句柄 ?
图形对象 一个图形包含了不同的对象 图形包括 核心对象和绘制对象 . 核心对象 线条对象 : line 文本对象 : text 矩形对象 : rectangle 补丁对象 : patch 图像对象 ...
- Split分割字符串
第一种方法:打开vs.net新建一个控制台项目.然后在Main()方法下输入下面的程序. string s="abcdeabcdeabcde"; string[] sArray=s ...
- hdu4686 Arc of Dream 2013 Multi-University Training Contest 9矩阵快速幂
Arc of Dream Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Tot ...
- 初识Hibernate之关联映射(一)
上篇文章我们对持久化对象进行的学习,了解了它的三种不同的状态并通过它完成对数据库的映射操作.但这都是基于单张表的操作,如果两张或者两张以上的表之间存在某种关联,我们又该如何利用持久化对象进行操作呢?本 ...