oracle-游标-存储过程-函数-包
一、存储过程
不可以在insert,update,delete中直接使用,可以有return但代表的是退出过程
过程有三种类型:不返回值,可以返回多个值,参数有三种类型,分别如下:
in:只输入,不返回结果,默认为in
out:只返回结果,不输入,要想取出输出变量的值必须通过pl/sql块的变量取出
in out:可输入,又可返回结果,要想取出输出变量的值必须通过pl/sql块的变量取出
--语法
create or replace procedure 名称(a1 in varchar2,a2 out varchar2,a3 in out int)
as begin end;
--测试in
create or replace procedure getSex1(p_customerName in varchar2)
as
v_sex varchar2(20);
begin
select sex into v_sex from customer where customerName=p_customerName;
dbms_output.put_line(v_sex);
end;
--测试out
create or replace procedure getSex2(p_customerName in varchar2,p_sex out varchar2)
as
begin
select sex into p_sex from customer where customerName like p_customerName;
exception
when others then raise_application_error(-20001,'occur error');
end;
--测试in out
create or replace procedure getSex3(p_param1 in out varchar2)
as
begin
select sex into p_param1 from customer where customerName=p_param1;
end;
--在pl/sql块中执行
declare
begin
getSex1('a1');
end; --在pl/sql块中执行
declare
p_sex varchar2(20);
begin
getSex2('a1',p_sex);
dbms_output.put_line(p_sex);
getSex2('a2',p_sex);
dbms_output.put_line(p_sex);
end; declare
p_sex varchar2(20);
begin
p_sex:='a1';
getSex3(p_sex);
dbms_output.put_line(p_sex);
p_sex:='a2';
getSex3(p_sex);
dbms_output.put_line(p_sex);
end;
二、函数:
只能返回且必须返回一个结果,可以直接用在insert,update,delele,select中
可以有多个return;
语法:
create or replace function 函数名(p1 varchar2) return varchar2
as
begin
exception
end;
实例:根据姓名返回姓别
create or replace function getSex(p_customerName varchar2) return varchar2
as
v_sex customer.sex%type;
begin
select sex into v_sex from customer where customerName=p_customerName;
return v_sex;
end;
执行函数:
1、在sql(sqlplus)
select getSex(customerName),sex from customer;
2、在pl/sql块中
declare
v_sex customer.sex%type;
begin
v_sex:=getsex('a1');
dbms_output.put_line(v_sex);
end; 存储过程:
1.声明处没有return,可以返回多个值,用输出变量(out,in out)返回
return代表退出程序,不返回结果.
2.不可以在insert,update,delete,select
中直接使用,只能通过exec中用
函数:
1.声明处有return,只可以返回一个值,
return代表返回一个值.
2.可以在insert,update,delete,select
中直接使用. 三、触发器
set serveroutput on;
create or replace trigger teacher_trigger after insert or update or delete on customer for each row
begin
dbms_output.put_line('the table data already has been modified....');
end; 四、游标
declare
v_customer customer%rowtype;
v_customerName customer.customerName%type;
cursor c1(v_customerName varchar2) is select * from customer where customerName like v_customerName;
begin
v_customerName:='&aa';
open c1(v_customerName);
fetch c1 into v_customer;
while(c1%found) loop
dbms_output.put_line(v_customer.customerName||' '||v_customer.sex);
fetch c1 into v_customer;
end loop;
end; 五、包
包:包中可以有多个方法,包包括包声明与包主体,包声明中声明的方法名,参数名,类型,个数必须与包主体的方法完全一样。
包声明中声明的变量是全局变量,大家都可以用
--实现包声明
create or replace package my_p
as
function getReverse(v_name varchar2) return varchar2;
procedure teacher_modify_column(teacher_id number,column_name varchar2,column_value1 number);
end; create or replace package body my_p
as
procedure teacher_modify_column(teacher_id number,column_name varchar2,column_value1 number)
is
v_sql varchar2(200);
begin
v_sql:='update teachers set '||column_name||' = '||column_value1||' where teachers.teacher_id = '||teacher_id;
EXECUTE IMMEDIATE v_sql;
end teacher_modify_column; function getReverse(v_name varchar2) return varchar2
as
v_title varchar2(200);
i int:=1;
j int:=0;
begin
j:=length(v_name);
while (j>0) loop
v_title:= substr(v_name,i,1)||v_title;
i:=i+1;
j:=j-1;
end loop;
return v_title;
end;
end; create or replace package MyPackage as
type c_type is ref cursor;
function MyReverse(source varchar2) return varchar2;
procedure splitPage(p_sql varchar2,page int,pageSize int,result out c_type,pageCount out int);
end;
--实现包主体
create or replace package body MyPackage as
function MyReverse(source varchar2) return varchar2
as
i int;
j int;
result varchar2(2000):='';
begin
j:=length(source);
i:=1;
while(i<=j) loop
result:=substr(source,i,1)||result;
i:=i+1;
end loop;
return result;
end;
// 查询语句 页数
procedure splitPage(p_sql varchar2,page int,pageSize int,result out c_type,pageCount out int)
as
v_sql varchar2(500);
startPage int;
endPage int;
v_rowCount int;
begin
v_sql:='select count(*) from ('||p_sql||')';
dbms_output.put_line(v_sql);
execute immediate v_sql into v_rowCount;
pageCount:=ceil(v_rowCount/pageSize);
if(page=0) then
raise_application_error(-20001,'申请的页面太小');
end if;
if(page>pageCount) then
raise_application_error(-20001,'申请的页面太大');
end if;
startPage:=(page-1)*pageSize;
endPage:=page*pageSize;
v_sql:='select * from ('||p_sql||') where rowNum<='||to_char(endPage);
v_sql:=v_sql||' minus ';
v_sql:=v_sql||'select * from ('||p_sql||') where rowNum<='||to_char(startPage);
dbms_output.put_line(v_sql);
open result for v_sql;
end;
end;
--调用包中的方法
select MyPackage.MyReverse(sex) from customer;
--在pl/sql块中调用
declare
pageCount int;
c1 mypackage.c_type;
v_customer customer%rowtype;
begin
mypackage.splitPage('select * from customer',1,2,c1,pageCount);
dbms_output.put_line('总页数是'||pageCount);
fetch c1 into v_customer;
while(c1%found) loop
dbms_output.put_line(v_customer.customername||' '||v_customer.sex);
fetch c1 into v_customer;
end loop;
close c1;
mypackage.splitPage('select * from customer',2,2,c1,pageCount);
fetch c1 into v_customer;
while(c1%found) loop
dbms_output.put_line(v_customer.customername||' '||v_customer.sex);
fetch c1 into v_customer;
end loop;
close c1;
mypackage.splitPage('select * from customer',3,2,c1,pageCount);
fetch c1 into v_customer;
while(c1%found) loop
dbms_output.put_line(v_customer.customername||' '||v_customer.sex);
fetch c1 into v_customer;
end loop;
close c1; mypackage.splitPage('select * from customer',4,2,c1,pageCount);
fetch c1 into v_customer;
while(c1%found) loop
dbms_output.put_line(v_customer.customername||' '||v_customer.sex);
fetch c1 into v_customer;
end loop;
close c1;
end;
oracle-游标-存储过程-函数-包的更多相关文章
- oracle上课 学习2 oracle 游标 存储过程 有用
1.1. 训练描述 使用游标,打印emp中20号部门的所有员工的信息 操作步骤答案 declare cursor c_emp is select * from emp where deptno=10 ...
- ArcSDE给Oracle添加SDE函数包
SDE函数包中包含大量的空间计算分析函数,是我们做空间相关分析的一把利刃(目前好像我们只有这一把),有关SDE函数的使用,请见我空间另外的一篇帖子的附件.按照通常教程,过程是这样的1.找到listen ...
- .Net程序员学用Oracle系列(7):视图、函数、存储过程、包
1.视图 1.1.创建.删除及调用普通视图 1.2.高级视图介绍 2.函数 2.1.系统函数介绍 2.2.创建.删除及调用自定义函数 3.存储过程 3.1.创建.修改及删除存储过程 3.2.调用存储过 ...
- .Net程序员学用Oracle系列:视图、函数、存储过程、包
1.视图 在实际操作过程中,本人发现 Oracle 视图定义有一个缺陷,就是不大方便注释,每次写好的注释执行之后再打开视图定义所有注释就全都没了.后来我发现把注释写到末尾就不会被清除,但这样总感觉乖乖 ...
- Oracle学习2 视图 索引 sql编程 游标 存储过程 存储函数 触发器
---视图 ---视图的概念:视图就是提供一个查询的窗口,来操作数据库中的数据,不存储数据,数据在表中. ---一个由查询语句定义的虚拟表. ---查询语句创建表 create table emp a ...
- java下实现调用oracle的存储过程和函数
在Oracle下创建一个test的账户,然后 1.创建表:STOCK_PRICES --创建表格 CREATE TABLE STOCK_PRICES( RIC VARCHAR() PRIMARY KE ...
- 编程开发之--Oracle数据库--存储过程和存储函数(2)
上一小结我们简单介绍了存储过程和存储函数,对存储过程和存储函数有了一个基本的了解,接下来介绍在java程序中如何调用我们创建的存储过程和存储函数 1.在应用程序中调用我们的存储过程 创建一个简单的Ja ...
- [转]SQLServer和Oracle,存储过程区别,常用函数对比
本文转自:http://www.cnblogs.com/neru/archive/2011/08/18/2144049.html 以前一直用sqlserver,只有很少的一点oracle的经验,现在要 ...
- MySQL存储过程中的3种循环,存储过程的基本语法,ORACLE与MYSQL的存储过程/函数的使用区别,退出存储过程方法
在MySQL存储过程的语句中有三个标准的循环方式:WHILE循环,LOOP循环以及REPEAT循环.还有一种非标准的循环方式:GOTO,不过这种循环方式最好别用,很容易引起程序的混乱,在这里就不错具体 ...
随机推荐
- gitlab安装教程 正在修炼的西瓜君
查看内存配置 我们先不急着来安装gitlab,先来看一下自己电脑的内存情况,我把这一步提到最前面,是因为这是我安装过程中遇到的最大的坑. 下面是gitlab的cpu和内存需求(https://do ...
- C# 设置鼠标光标位置
C# 设置鼠标光标位置 using System.Drawing; using System.Runtime.InteropServices; namespace ZB.QueueSys.Common ...
- Canal的简单使用(监控数据库数据的变化)
原文:https://www.cnblogs.com/java-spring/p/8930740.html canal可以用来监控数据库数据的变化,从而获得新增数据,或者修改的数据,用于实际工作中,比 ...
- 标准键盘 acsii码值 表
码值 含义 备注 0x08 Backspace键 0x09 Tab键 0x0C Clear键 Num Lock关闭时的数字键盘5 0x0D Enter键 0x10 Shift键 0x1 ...
- NLP学习(1)---Glove模型---词向量模型
一.简介: 1.概念:glove是一种无监督的Word representation方法. Count-based模型,如GloVe,本质上是对共现矩阵进行降维.首先,构建一个词汇的共现矩阵,每一行是 ...
- linux tar打包压缩排除某个目录或文件
用tar打包时想剔除打包目录中的某个子目录或文件: 比如你想打包/home这个目录,但是/home/afish/目录和/home/www/afish.php文件你都不想打包,方法是: tar -zcv ...
- evpp http put问题
https://blog.csdn.net/yuzuyi2006/article/details/82112664 最近做了一个项目需要实现web服务,使用了evpp.但是在用的过程中碰到了http ...
- Vue -- element-ui el-table 点击tr项页面跳转,返回后缓存回显点击项
页面跳转反显(点击项,点击table滚动的位置,搜索条件,分页回显) 点击table tr项后,页面跳转到下级页面,返回回显搜索条件.当前页码.并将点击项select选中.滚动条也被记录回显跳转时滚动 ...
- 优化编辑器的编程语言 mlton
MLton 是整个程序的优化编译器的标准ML编程语言.
- python自动华 (十)
Python自动化 [第十篇]:Python进阶-多进程/协程/事件驱动与Select\Poll\Epoll异步IO 本节内容: 多进程 协程 事件驱动与Select\Poll\Epoll异步IO ...