Oracle 的存储过程及游标_第1页
Oracle 的存储过程及游标_第2页
Oracle 的存储过程及游标_第3页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Oracle 的存储过程及游标Oracle 中的存储过程和游标:select myFunc(1,2.) to dual;一:OracleA:函数必须有返回值,而过程没有.BexecuteCSQL其实我们可以将比较复杂的查询写成函数.然后到存储过程中去调用这些函数. 二:如何创建存储过程:A:格式create or replace procedure (参数名参数类型以及描述,.) -注意,没有返回值is变量声明 begin过程处理;null;exceptionwhen 异常名 then end;inoutin out点和函数一样.B:举例 1:create or replace procedu

2、re myPro-create or replace proc myPro 出错 不能简写(a in int:=0,b in int:=0) isc int:=0; begin c:=a+b;dbms_output.put_line(C is value|c); end;execute myPro(10,20);Sql Serverexec myPro(10,20); -可以简写C:举例 2:SelectSelectinto,程同样也需要.create or replace procedure myPro1 (a int:=0,b int:=0)isc int:=0; beginselect

3、empno+a+b into c from emp where ename=FORD; dbms_output.put_line(C is values |c);end;execute myPro1(10,20)D:假如在一个过程里面要返回一个结果集,怎么办?大家注意.就必须要用到游标了!用游标来处理这个结果集.create or replace procedure Test (varEmpName emp.ename%type)is begin -会报错.错误原因没有 into 子句.select * from emp where ename like %|varEmpName|%; end

4、;into,Oracle集.这个时候我们可以声明游标对象去接受他.PL/SQL 游标:A:分类:1cursor字.2:显示游标:用户明确通过 cursor 关键字来声明的游标.B:什么是隐式游标:1:什么时候产生:会在执行任何合法的SQL语句(DML-INSERT UPDATE DELETE DQLSELECT)中产生.他不一定存放数据.也有可能存放记录集所影响的行数.SELECTINSERT DELETEC:隐式游标叫什么名字:名字叫 sql关于 sql 的游标变量到底有哪些呢?SQLSQL SQLsql%foundsql%notfound sql%rowcount sql%isopenD:

5、关于隐式游标的例子:create table 学生基本信息表(StuID int,StuName varchar2(20)alter table 学生基本信息表 add constraint PK_STUID primary key(StuID) declarenum int:=0; begin num:=#delete from 学生基本信息表 where StuID=num; if sql%notfound thendbms_output.put_line(该行数据没有发现); elsedbms_output.put_line(数据被发现并删除,影响的行数为:|sql%rowcou

6、nt);end if; end;E:关于显示游标的例子:1:如何定义显示游标declare cursor is select 语句;declare cursor mycur is select empno,ename,job from scott.emp; 2:如何打开游标:open ; open mycur;3:如何通过游标来读取数据fetch into 4:如何关闭游标:close ; close mycur;OracleOracleJAVA写的.Oracle5:举例:declarecursor mycur is select empno,ename,job from emp; vempn

7、o emp.empno%type;vename emp.ename%type; vjob emp.job%type; beginopen mycur;fetch mycur into vempno,vename,vjob; dbms_output.put_line(I Found You!|mycur%rowcount|dbms_output.put_line(读取的数据为|vempno|vename|vjob); close mycur;end;因为只读出来一条,所以要遍历一下:declarecursor mycur is select empno,ename,job from emp; v

8、empno emp.empno%type;vename vjob emp.job%type; beginopen loopfetch mycur into vempno,vename,vjob; exit when mycur%notfound;if mycur%found thendbms_output.put_line(读取的数据为|vempno|vename|vjob); end if;end loop;dbms_output.put_line(I Found You!|mycur%rowcount|行); close mycur;end;6:通常情况下我们在读取表数据的时候,我们需要动

9、态的去查询.所以能不能在Oracle 中给游标带参数呢?可以!:如何定义带参数的游标:declare cursor (参数名称 参数类型描述) is select xxxxx from bbbbb where aaa=? and ccc=?;:例子:游标是一个集合,读取数据有两种方式第一种方式: open fetch close第二种方式: for 一但使用了 for 循环 在循环刚刚开始的时候,相当于执行openclose declarecursor query(vname varchar) is select empno,ename,job from emp where ename like%|vname|%;beginfor line in query(A) loopdbms_output.put_line(line.empno| |line.ename| |line.job);end loop; end;实现动态输入:declarecursor query(vname varchar) is select empno,ename,job from emp where ename like%|v

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论