一、存储过程

  存储过程是一组为了完成特定功能的SQL 语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它。oracle可以把PL/SQL程序储存在数据库中,并可以在任何地方来运行它。存储过程被称为PL/SQL子程序,是被命名的PL/SQL快,存储在数据库,通过输入、输出参数与调用者交换信息。oracle存储过程不返回数据。

  语法:

  create or replace procudure 存储过名称(  
    参数名称  输入输出类型  参数类型,    
    参数名称  输入输出类型  参数类型  
  )   
  is
  begin
    处理语句;
    exceeption;
      异常处理语句;
  end 存储过名称;

  输出输出类型有如下三种:  

  • IN 定义一个输入参数变量,用于传递参数给存储过程,存储过程无法改变参数值,该参数可以是常量、或是有值的变量。
  • OUT 定义一个输出参数变量,用于从存储过程获取数据,该参数必须是一个变量,该变量是否有值不重要。
  • IN OUT 定义一个输入、输出参数变量,兼有以上两者的功能,该参数必须是一个变量,该变量必须有值。

  输出输出参数类型一般不声明长度,因为对于IN参数,其宽度是由外部决定。 对于OUT 和IN OUT 参数,其宽度是由存储过程内部决定。对于没有说明输入输出类型的参数,默认为IN类型。

二、示例

  以下代码person表结构如下:

DROP TABLE person ;
CREATE TABLE person (
id NUMBER(11) NOT NULL ,
username VARCHAR2(255 ) NULL ,
age NUMBER(11) NULL ,
password VARCHAR2(255) NULL ,
PRIMARY KEY (id)
)
INSERT INTO person VALUES ('', '张三', '', 'zhang123');
INSERT INTO person VALUES ('', '李四', '', 'lisi123');
INSERT INTO person VALUES ('', '王五', '', 'wang123');
INSERT INTO person VALUES ('', '赵六', '', 'zhao123');

  1、查询一个(in、out)

create or replace procedure pro_person_getbyid(
p_id in number,
p_username out varchar2,
p_age out number,
p_password out varchar2
)
is
begin
select username, age, password into p_username, p_age, p_password from person where id = p_id;
end pro_person_getbyid;
-- 调用代码 --------------
declare
v_id number;
v_username varchar2(255);
v_age number;
v_password varchar2(255);
begin
v_id := 1;
pro_person_getbyid(v_id, v_username, v_age, v_password);
dbms_output.put_line('username:'||v_username||' age:'||v_age||' password:'||v_password);
end;

  2、查询一个(in、out)使用rowtype

create or replace procedure pro_person_getrow(
p_id in number,
p_row out person%rowtype, -- rowtype类型变量
p_count out number -- 标记是否找到记录
)
is
begin
select * into p_row from person where id = p_id;
p_count := SQL%ROWCOUNT;
exception
when no_data_found then
p_count := 0;
end pro_person_getrow;
-- 调用--------------
declare
v_id number := 28;
v_row person%rowtype;
v_count number;
begin
pro_person_getrow(v_id, v_row, v_count);
dbms_output.put_line(v_count);
dbms_output.put_line('id:'||v_row.id||' username:'||v_row.username||' age:'||v_row.age||' password:'||v_row.password);
end;

  3、添加记录(in、out)

create or replace procedure pro_person_insert(
p_id number,
p_username varchar2,
p_age number,
p_password varchar2,
p_count out number -- 是否添加成功
)
is
begin
insert into person (id, username, age, password) values(p_id, p_username, p_age, p_password);
p_count := SQL%ROWCOUNT; -- SQL%ROWCOUNT为 隐式游标的属性
commit;
exception
when others then
p_count := 0; -- 失败
end pro_person_insert; -- 调用procedure
declare
v_id number := 28;
v_username varchar2(255) := 'xiaoli';
v_age number := 19;
v_password varchar2(255) := 'xiao123';
v_count number;
begin
pro_person_insert(p_id => v_id, p_username => v_username, p_age => v_age, p_password => v_password, p_count => v_count);
-- pro_person_insert(v_id , v_username, v_age, v_password, v_count);
dbms_output.put_line('影响行数'||v_count);
end;

  4、更新(in、out)

create or replace procedure pro_person_update(
p_id number,
p_age number,
p_password varchar2,
p_count out number
)
is
begin
update person set age = p_age, password = p_password where id = p_id;
p_count := SQL%ROWCOUNT;
commit;
exception
when no_data_found then
p_count := 0;
when others then
p_count := -1;
end pro_person_update;
-- 调用---------------------
declare
v_id number := 28;
v_age number := 19;
v_password varchar2(255) := 'password';
v_count number;
begin
pro_person_update(v_id, v_age, v_password, v_count);
dbms_output.put_line('影响行数'||v_count);
end;

  5、删除(in、out)

create or replace procedure pro_person_delete(
p_id number,
p_count out number
)
is
begin
delete from person where id = p_id;
p_count := SQL%ROWCOUNT;
commit;
exception
when no_data_found then
p_count := 0;
when others then
p_count := -1;
end pro_person_delete;
-- 调用----------------
declare
v_id number := 28;
v_count number;
begin
pro_person_delete(v_id, v_count);
dbms_output.put_line('影响行数'||v_count);
end;

  6、查询所有(in、out)使用sys_refcursor

create or replace procedure pro_person_findall2(
p_cursor out sys_refcursor -- 输出参数为包类型
)
is
begin
open p_cursor for
select * from person;
exception
when others then
DBMS_OUTPUT.PUT_LINE('获取信息发生错误');
end pro_person_findall2; ----调用---------------------------------------------------
declare
c_cursor sys_refcursor;
r_person person%rowtype;
begin
pro_person_findall2(c_cursor);
--2、打开游标
-- open c_cursor; --此处不需要显示地打开游标,因为调用存储过程的时候返回的游标已经打开了
--3、提取数据
loop
fetch c_cursor
into r_person;
exit when c_cursor%notfound; -- 下面没有数据的时候,退出
dbms_output.put_line('id:'||r_person.id);
dbms_output.put_line('username:'||r_person.username);
dbms_output.put_line('age:'||r_person.age);
end loop;
end;

  7、查询所有(in、out)使用自定义类型查询

-- 创建一个包类型
create or replace package pkg_const as
type r_cursor is ref cursor;
end pkg_const; -- 创建存储过程,
create or replace procedure pro_person_findall(
p_cursor out pkg_const.r_cursor -- 输出参数为包类型
)
is
begin
open p_cursor for
select * from person;
exception
when others then
DBMS_OUTPUT.PUT_LINE('获取信息发生错误');
end pro_person_findall; ----调用------------------------------------
declare
c_cursor pkg_const.r_cursor;
r_person person%rowtype;
begin
pro_person_findall(c_cursor);
--2、打开游标
-- open c_cursor;
--3、提取数据
loop
fetch c_cursor
into r_person;
exit when c_cursor%notfound; -- 下面没有数据的时候,退出
dbms_output.put_line('id:'||r_person.id);
dbms_output.put_line('username:'||r_person.username);
dbms_output.put_line('age:'||r_person.age);
end loop;
end;

三、存储过程其他语句

  查看存储过程

DESCRIBE 存储过程名;

  删除存储过程

DROP PROCEDURE 存储过程名;

PL/SQL之--存储过程的更多相关文章

  1. PL/SQL之存储过程和函数

    1.创建存储过程 .1语法: CREATE[OR REPLACE] PROCEDURE [schema.] procedure_name[(argument[{IN|OUT|IN OUT}] data ...

  2. PL/SQL编程—存储过程

    SQL> create or replace procedure sp_pro3(name_in varchar2,id_in varchar2) is begin update mytest ...

  3. oracle 学习(五)pl/sql语言存储过程&包

    首先搞清楚俩概念 存储过程(procedure)&程序包(package) 存储过程:数据库对象之一,可以理解为数据库的子程序,在客户端和服务器端可以直接调用它.触发器是与表直接关联的特殊存储 ...

  4. PL/SQL出现存储过程注释中文乱码

    进入PL/SQL命令行窗口输入:select userenv('language') from dual 查出数据库字符集 输入:select * from V$NLS_PARAMETERS 查出NL ...

  5. 使用PL/SQL编写存储过程访问数据库

    一.实验目的 熟悉使用存储过程来进行数据库应用程序的设计. 二.实验内容 对学生-课程数据库,编写存储过程,完成下面功能: 1.统计离散数学的成绩分布情况,即按照各分数段统计人数: 2.统计任意一门课 ...

  6. PL/SQL通过存储过程为相同数据添加序号

    在Oracle数据库中存有一串数据(Ori_Seq),数据包含不等量重复: 为方便查看与管理,现希望添加一字段(New_Seq),在原有数据的末尾为其添加一串序号,相同数据序号从小到大排列,序号长度为 ...

  7. pl/sql调试存储过程

    1.找到对应的存储过程 2.在存储过程名称上右键,选择Test 3.点击1标识的按钮(begin debugger),选择2开始调试 4.存储过程如需参数,需要在右侧下方的表格区域(3)填入对应的值即 ...

  8. PL/SQL之存储过程和触发器实例

    1.Oracle存储过程实例 /*不带任何参数存储过程(输出系统日期)*/ CREATE OR REPLACE PROCEDURE output_date IS BEGIN DBMS_OUTPUT.P ...

  9. PL/SQL 05 存储过程 procedure

    --存储过程(不带参数) create or replace procedure 存储过程名as  变量.常量声明;begin  代码;end; --存储过程(带输入参数) create or rep ...

随机推荐

  1. UINavigationItem和UItabBarItem的区别详解

    一.UINavigationItem 1> 获得方式 self.navigationItem // self是指控制器 2> 作用 可以用来设置当前控制器顶部导航栏的内容 // 设置导航栏 ...

  2. IOS开发UI基础文本属性Attributes

    文本属性Attributes 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont systemFo ...

  3. Spring基础——在 Spring Config 文件中基于 XML 的 Bean 的自动装配

    一.Spring IOC 容器支持自动装配 Bean,所谓自动装配是指,不需要通过 <property> 或 <constructor-arg> 为 Bean 的属性注入值的过 ...

  4. 使用Windows PE的U盘安装win7

    前年刚去公司的时候用PE装过好多系统,最近又装一台华硕的,碰到了一个问题,一起记录了下. 华硕X45,Bios已经改为U盘启动了,但就是进不去,因为知道可能还有个选磁盘启动项的键,找了半天原来按Esc ...

  5. 找到MVC框架中前端URL与后端同步的解决方案

    基本思路: 先用URL标签生成完整的URL字符,前端动态参数的部分以适配符先填充,最后动态参数利用正则匹配进行替换. 这种方式,可以在各种MVC框架中适用,妙. 不废话,上码. var url = & ...

  6. strcpy函数实现

    1,strcpy最简便实现 char * strcpy_to (char *dst, const char *src) { char *address = dst; assert((dst != NU ...

  7. nginx配合modsecurity实现WAF功能

    一.准备工作 系统:centos 7.2 64位.nginx1.10.2, modsecurity2.9.1 owasp3.0 1.nginx:http://nginx.org/download/ng ...

  8. Linux Shell系列教程之(十五) Shell函数简介

    本文是Linux Shell系列教程的第(十五)篇,更多Linux Shell教程请看:Linux Shell系列教程 函数可以将一个复杂功能划分成若干模块,从而使程序结构更加清晰,代码重复利用率更高 ...

  9. ThoughtWorks.QRCode生成二维码

    首先引用需要的dll,此处使用的是ThoughtWorks.QRCode.dll,网上可以找到对应的,此处也有一份,点击下载 http://files.cnblogs.com/files/ives/T ...

  10. mysql init-file参数中语句限制

    mysql 启动选项中的init-file文件的内容目测只能是dml语句,不能包含ddl,否则执行就会报错,但不影响启动本身..太扯了..