PL/SQL之--存储过程
一、存储过程
存储过程是一组为了完成特定功能的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之--存储过程的更多相关文章
- PL/SQL之存储过程和函数
1.创建存储过程 .1语法: CREATE[OR REPLACE] PROCEDURE [schema.] procedure_name[(argument[{IN|OUT|IN OUT}] data ...
- PL/SQL编程—存储过程
SQL> create or replace procedure sp_pro3(name_in varchar2,id_in varchar2) is begin update mytest ...
- oracle 学习(五)pl/sql语言存储过程&包
首先搞清楚俩概念 存储过程(procedure)&程序包(package) 存储过程:数据库对象之一,可以理解为数据库的子程序,在客户端和服务器端可以直接调用它.触发器是与表直接关联的特殊存储 ...
- PL/SQL出现存储过程注释中文乱码
进入PL/SQL命令行窗口输入:select userenv('language') from dual 查出数据库字符集 输入:select * from V$NLS_PARAMETERS 查出NL ...
- 使用PL/SQL编写存储过程访问数据库
一.实验目的 熟悉使用存储过程来进行数据库应用程序的设计. 二.实验内容 对学生-课程数据库,编写存储过程,完成下面功能: 1.统计离散数学的成绩分布情况,即按照各分数段统计人数: 2.统计任意一门课 ...
- PL/SQL通过存储过程为相同数据添加序号
在Oracle数据库中存有一串数据(Ori_Seq),数据包含不等量重复: 为方便查看与管理,现希望添加一字段(New_Seq),在原有数据的末尾为其添加一串序号,相同数据序号从小到大排列,序号长度为 ...
- pl/sql调试存储过程
1.找到对应的存储过程 2.在存储过程名称上右键,选择Test 3.点击1标识的按钮(begin debugger),选择2开始调试 4.存储过程如需参数,需要在右侧下方的表格区域(3)填入对应的值即 ...
- PL/SQL之存储过程和触发器实例
1.Oracle存储过程实例 /*不带任何参数存储过程(输出系统日期)*/ CREATE OR REPLACE PROCEDURE output_date IS BEGIN DBMS_OUTPUT.P ...
- PL/SQL 05 存储过程 procedure
--存储过程(不带参数) create or replace procedure 存储过程名as 变量.常量声明;begin 代码;end; --存储过程(带输入参数) create or rep ...
随机推荐
- 一篇通俗易懂的讲解OpenGL ES的文章
电脑或者手机上做图像处理有很多方式,但是目前为止最高效的方法是有效地使用图形处理单元,或者叫 GPU.你的手机包含两个不同的处理单元,CPU 和 GPU.CPU 是个多面手,并且不得不处理所有的事情, ...
- 使用MinGW 编译 iconv 库
原文链接: http://www.code-by.org/viewtopic.php?f=54&t=166 GNU页面 http://ftp.gnu.org/pub/gnu/libiconv/ ...
- AC自动机 - 关于Fail指针
fail指针可以说是AC自动机里最难理解的东西,怎样更好的理解AC自动机的fail指针? 先来看一幅图: 看这幅图上的fail指针是怎么构造的. 树上的词分别是: { he , hers , his ...
- 安装 Oracle P6 EPPM 16 R1 database for 12C A
- asp.net MVC ajax上传文件
普通上传 view: <body> <form id="form1" method="post" action="@Url.Acti ...
- 在aspx怎么引用public string getPicurl(string picurl)?
刚才在论坛上看到一帖: Insus.NET尝试做了一下,直接使用一个Img标签是无法实现.因为函数中返回的即是一个img html标签,因此在aspx页再不能使用Img了. 现在可以回到网友的问题,那 ...
- Python基础:序列
一.概述 序列类型(Sequence Types)类似于C中的数组,它是包含一组成员的连续列表,这些成员紧邻排列,并且可以通过序号(下标偏移量)访问其中的一个或多个成员.序列类型的示意图如下所示: P ...
- 【Unity】13.2 通过Lighting Window设置相关参数
分类:Unity.C#.VS2015 创建日期:2016-05-19 一.简介 Unity 5.3.4的Lighting Window有3个选项卡:Object.Scene.Lightmaps. 二. ...
- csharp: Sound recording
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsof ...
- 我的Machine Learning学习之路
从2016年年初,开始用python写一个简单的爬虫,帮我收集一些数据. 6月份,开始学习Machine Learning的相关知识. 9月开始学习Spark和Scala. 现在想,整理一下思路. 先 ...