1.过程【存储过程】
CREATE [OR REPLACE] PROCEDURE
<procedure name> [(<parameter list>)]
IS|AS
<local variable declaration>
BEGIN
<executable statements>
[EXCEPTION
<exception handlers>]
END; 1.1无参存储过程
写一个存储过程:实现向student表中插入10条记录的功能
create or replace procedure pro_student
is
--声明变量
begin
-- 执行逻辑代码
for i in 950..980 loop
insert into student(id,name) values(i,'asasas');
commit;
end loop;
--异常处理
end; --注意:选中过程代码按齿轮提交并不是执行代码而是编译代码并保存在数据库中
-- 保存在左侧菜单栏中的 procedures目录中
调用存储过程:
1.选中过程名称右键‘测试’选项执行
2.通过plsql块调用 begin
pro_student();--如果没有参数 ()可以省略
end;
1.2 有参存储过程
create or replace procedure pro_student2(
p_id in number,-- 默认就是in 可以省略
p_name in varchar2, --没有被out修饰的参数是常量,不能被改变
p_result out student%rowtype -- 接收返回信息 )
is
begin
select * into p_result from student where id=p_id and name=p_name;
end; declare
v_id student.id%type := '&请输入id';
v_name student.name%type :='&请输入姓名';
v_student student%rowtype;
begin
pro_student2(v_id,v_name,v_student);
dbms_output.put_line(v_student.id);
dbms_output.put_line(v_student.name);
dbms_output.put_line(v_student.sex);
dbms_output.put_line(v_student.birth);
dbms_output.put_line(v_student.address); end; 案例:请根据性别或名字查询相关记录,并把结果 返回来 打印了出来 提示用 sys_refcursor create or replace procedure pro_student3(
v_name in student.name%type,
v_sex in student.sex%type,
mycursor out sys_refcursor
)
is
v_sql varchar2(100);
begin v_sql:='select * from student where 1=1';
if v_name is not null then
v_sql := v_sql || 'name like ''%'||v_name||'%''';
end if;
if v_sex is not null then
v_sql := v_sql || 'sex = '''||v_sex||'''';
end if;
dbms_output.put_line(v_sql);
open mycursor for v_sql;
close mycursor;
end; declare
v_cursor sys_refcursor;
v_name student.name%type:='&请输入姓名';
v_sex student.sex%type:='&请输入性别';
v_student student%rowtype;
begin
pro_student3(v_name,v_sex,v_cursor); loop
fetch v_cursor into v_student;
exit when v_cursor%notfound;
dbms_output.put_line(v_student.id||v_student.name);
end loop;
close v_cursor;
end; 2.函数【方法】:有显示的返回值
CREATE [OR REPLACE] FUNCTION
<function name> [(param1,param2)]
RETURN <datatype> IS|AS
[local declarations]
BEGIN
Executable Statements;
RETURN result;
EXCEPTION
Exception handlers;
END;
写一个方法实现根据班级编号获取班级名称
create or replace function func_class(
f_id number
)return varchar2
is
v_name t_class.cname%type;
begin
select cname into v_name from t_class where cid = f_id;
return v_name; exception
when no_data_found then
dbms_output.put_line('找不到数据');
return null;
when others then
dbms_output.put_line('其他异常');
return null;
end; 调用方法:
1.通过工具测试
2.通过plsql块执行
begin
dbms_output.put_line(func_class(1));
end;
3.通过SQL语句调用【方法中不能有DML操作】
select fun_test1(102) from dual;
select t.*,fun_test1(t.classid) from t_student t

方法和过程的区别:
1.DML相关的操作我们一般都使用存储过程实现
2.特定的公共的功能我们用方法实现
3.方法有显示的返回结果
4.方法中同样的也有 in out 关键字

create or replace package p_student is

  -- Author  : BOBO
-- Created : 2018-08-06 11:29:41
-- Purpose : -- Public type declarations 定义一个类型
--type <TypeName> is <Datatype>;
--type myreftype is ref cursor;
-- Public constant declarations -- 声明一个常量
--<ConstantName> constant <Datatype> := <Value>; -- Public variable declarations
--<VariableName> <Datatype>; -- 声明一个变量 -- Public function and procedure declarations
--function <FunctionName>(<Parameter> <Datatype>) return <Datatype>; procedure insert_student(
p_id t_student.id%type,
p_name t_student.name%type
); function getname(
p_id t_student.id%type
)return varchar2; end p_student; create or replace package body p_student is -- Private type declarations
--type <TypeName> is <Datatype>; -- Private constant declarations
--<ConstantName> constant <Datatype> := <Value>; -- Private variable declarations
--<VariableName> <Datatype>; -- Function and procedure implementations
-- function <FunctionName>(<Parameter> <Datatype>) return <Datatype> is
--<LocalVariable> <Datatype>;
-- begin
--<Statement>;
--return(<Result>);
-- end; --begin
-- Initialization
--<Statement>; procedure insert_student(
p_id t_student.id%type,
p_name t_student.name%type
)is
begin
insert into t_student(id,name)values(seq_t_student.nextval,p_name);
commit;
end; function getname1(
p_id t_student.id%type
)return varchar2
is
v_name t_student.name%type;
begin
select name into v_name from t_student where id = p_id;
return v_name;
end; function getname(
p_id t_student.id%type
)return varchar2
is
v_name t_student.name%type;
begin
select name into v_name from t_student where id = p_id;
return getname1(p_id);
end; end p_student;

不在package中声明,直接在body中定义实现,是为了隐藏方法,同时给内部的其他方法或者存储过程调用

Oracle之子程序(存储过程、方法、包)的更多相关文章

  1. .Net程序员学用Oracle系列(7):视图、函数、存储过程、包

    1.视图 1.1.创建.删除及调用普通视图 1.2.高级视图介绍 2.函数 2.1.系统函数介绍 2.2.创建.删除及调用自定义函数 3.存储过程 3.1.创建.修改及删除存储过程 3.2.调用存储过 ...

  2. .Net程序员学用Oracle系列:视图、函数、存储过程、包

    1.视图 在实际操作过程中,本人发现 Oracle 视图定义有一个缺陷,就是不大方便注释,每次写好的注释执行之后再打开视图定义所有注释就全都没了.后来我发现把注释写到末尾就不会被清除,但这样总感觉乖乖 ...

  3. java通过JDBC连接Oracle并调用存储过程和存储方法

    初始配置:电脑安装oracle 11g(这里也可使是其它版本也可,此教程演示为11g),java环境,eclipse,oracle关于jdbc的jar包. 一,在scott用户下首先要有存储过程和存储 ...

  4. oracle学习笔记(十九) 子程序——存储过程

    子程序--存储过程 我们可以使用子程序来封装一下我们需要的操作,子程序又有存储过程,函数和触发器. 这里先学习存储过程~ 语法 create [or replace] procedure $proce ...

  5. oracle中的存储过程例子

    用了两年Oracle还没写过存储过程,真是十分惭愧,从今天开始学习Oracle存储过程,完全零起点,争取每日一篇学习笔记,可能开始认识的不全面甚至有错误,但坚持下来一定会有收获. . 建立一个存储过程 ...

  6. Dapper完美兼容Oracle,执行存储过程,并返回结果集。

    Dapper完美兼容Oracle,执行存储过程,并返回结果集. 这个问题,困扰了我整整两天. 刚刚用到Dapper的时候,感觉非常牛掰.特别是配合.net 4.0新特性dynamic,让我生成泛型集合 ...

  7. Oracle中执行存储过程call和exec区别

    Oracle中执行存储过程call和exec区别 在sqlplus中这两种方法都可以使用: exec pro_name(参数1..); call pro_name(参数1..); 区别: 1. 但是e ...

  8. Oracle job procedure 存储过程定时任务

    Oracle job procedure 存储过程定时任务 oracle job有定时执行的功能,可以在指定的时间点或每天的某个时间点自行执行任务. 一.查询系统中的job,可以查询视图 --相关视图 ...

  9. Oracle dbms_lock.sleep()存储过程使用技巧-场景-分析-实例

    <Oracle dbms_lock.sleep()存储过程使用技巧>-场景-分析-实例 摘要:今天是2014年3月10日,北京,雾霾,下午组织相关部门开会.会议的结尾一名开发工程师找到了我 ...

随机推荐

  1. 打通版微社区(6):部署微信插件及开通QQ云服务

    写在前面: 此文是我最后写的.其实实际部署的时候,我是先安装了论坛并试图开通微信的微社区.发现微社区需要在微信公众平台的开发者中心里配置 "网页账号,网页授权获取用户基本信息"为论 ...

  2. 设计模式(16) 观察者模式(OBSERVER)C++实现

    意图: 定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新. 动机: 将一个系统设计成一系列相互协作的类有一个常见的副作用:需要维护相关对象之间的一 ...

  3. CentOS7使用systemctl添加自定义服务

    一.简介 Centos7开机第一个程序从init完全换成了systemd这种启动方式,同centos 5 6已经是实质差别.systemd是靠管理unit的方式来控制开机服务,开机级别等功能. 在/u ...

  4. 实用的 CSS 小片段

    看了 30 Seconds CSS,有了许多收获,所以写下了这篇文章,算是收藏一些代码小片段,留作后用. 一.手写 Loading 动画 (1)弹性加载动画 CSS 代码如下: .bounce-loa ...

  5. Codeforces-19D Point---线段树

    题目链接: https://cn.vjudge.net/problem/CodeForces-19D 题目大意: n个操作,在200000*200000的平面上加删点 find 严格在坐标右上角,x最 ...

  6. hook与链表操作

    hook与链表的节点操作有相似之处: 链表的前后顺序相当于程序的执行流: 对链表节点的替换或插入相当于hook技术: 1.替换:用新的节点替换原来的节点: 2.前插入: 3.后插入: 修改原来的结构.

  7. fill & stroke

    - (void)stroke Draws a line along the receiver’s path using the current drawing properties. - (void) ...

  8. Spring Security 自定义配置(1)

    @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapte ...

  9. 程序人生:02我来告诉你,一个草根程序员如何进入BAT

    本文摘自左潇龙博客,原文出处:http://www.cnblogs.com/zuoxiaolong/p/life54.html 引言 首先声明,不要再问LZ谁是林萧,林萧就是某著名程序员小说的主角名字 ...

  10. Python - if-else 的多种简洁写法

    本博客原文来自:http://www.cnblogs.com/xiexiaoxiao/p/7772441.html,对原作者表示感谢,此处个人转载. 1. 常用 if ... else写法 # 语法 ...