oracle存储过程详细使用手册

上传人:壹****1 文档编号:508652408 上传时间:2022-08-21 格式:DOC 页数:42 大小:165KB
返回 下载 相关 举报
oracle存储过程详细使用手册_第1页
第1页 / 共42页
oracle存储过程详细使用手册_第2页
第2页 / 共42页
oracle存储过程详细使用手册_第3页
第3页 / 共42页
oracle存储过程详细使用手册_第4页
第4页 / 共42页
oracle存储过程详细使用手册_第5页
第5页 / 共42页
点击查看更多>>
资源描述

《oracle存储过程详细使用手册》由会员分享,可在线阅读,更多相关《oracle存储过程详细使用手册(42页珍藏版)》请在金锄头文库上搜索。

1、Oracle存储过程总结1、创建存储过程create or replace procedure test(var_name_1 in type,var_name_2 out type) as-声明变量(变量名 变量类型)begin-存储过程的执行体end test;打印出输入的时间信息E.g:create or replace procedure protest1(workDate in Date) isbegindbms_output.put_line(to_date(workDate);end;2、变量赋值变量名 := 值;E.g:create or replace procedure t

2、est(workDate in Date) isx number(4,2);beginx := 1;end test;3、判断语句:if 比较式 then begin end; end if;E.gcreate or replace procedure test(y in number) isx number:=0;beginx:=y;if x 0 thenbeginx := 0 - x;end;end if;if x = 0 thenbeginx:=1; end;end if;end test;4、For 循环For . in . LOOP-执行语句end LOOP;(1)循环遍历游标cre

3、ate or replace procedure test() asCursor cursor is select name from student; name varchar(20);beginfor name in cursor LOOPbegindbms_output.putline(name);end;end LOOP;end test;(2)循环遍历数组create or replace procedure test(varArray in myPackage.TestArray) as-(输入参数varArray 是自定义的数组类型,定义方式见标题6)i number;begin

4、i := 1; -存储过程数组是起始位置是从1开始的,与java、C、C+等语言不同。因为在Oracle中本是没有数组的概念的,数组其实就是一张-表(Table),每个数组元素就是表中的一个记录,所以遍历数组时就相当于从表中的第一条记录开始遍历for i in 1.varArray.count LOOPdbms_output.putline(The No. | i |record in varArray is: |varArray(i);end LOOP;end test;5、While 循环while 条件语句 LOOPbeginend;end LOOP;E.gcreate or repla

5、ce procedure test(i in number) asbeginwhile i 10 LOOPbegini:= i + 1;end;end LOOP;end test;6、数组首先明确一个概念:Oracle中本是没有数组的概念的,数组其实就是一张表(Table),每个数组元素就是表中的一个记录。使用数组时,用户可以使用Oracle已经定义好的数组类型,或可根据自己的需要定义数组类型。(1)使用Oracle自带的数组类型x array; -使用时需要需要进行初始化e.g:create or replace procedure test(y out array) isx array;b

6、eginx := new array();y := x;end test;(2)自定义的数组类型 (自定义数据类型时,建议通过创建Package的方式实现,以便于管理)E.g (自定义使用参见标题4.2) create or replace package myPackage is - Public type declarations type info is record(name varchar(20), y number); type TestArray is table of info index by binary_integer; -此处声明了一个TestArray的类型数据,其实

7、其为一张存储Info数据类型的Table而已,及TestArray 就是一张表,有两个字段,一个是name,一个是y。需要注意的是此处使用了Index by binary_integer 编制该Table的索引项,也可以不写,直接写成:type TestArray is table of info,如果不写的话使用数组时就需要进行初始化:varArray myPackage.TestArray; varArray := new myPackage.TestArray();end TestArray;7.游标的使用 Oracle中Cursor是非常有用的,用于遍历临时表中的查询结果。其相关方法和

8、属性也很多,现仅就常用的用法做一二介绍:(1)Cursor型游标(不能用于参数传递)create or replace procedure test() iscusor_1 Cursor is select std_name from student where .; -Cursor的使用方式1 cursor_2 Cursor;beginselect class_name into cursor_2 from class where .; -Cursor的使用方式2可使用For x in cursor LOOP . end LOOP; 来实现对Cursor的遍历end test;(2)SYS_

9、REFCURSOR型游标,该游标是Oracle以预先定义的游标,可作出参数进行传递create or replace procedure test(rsCursor out SYS_REFCURSOR) iscursor SYS_REFCURSOR; name varhcar(20);beginOPEN cursor FOR select name from student where . -SYS_REFCURSOR只能通过OPEN方法来打开和赋值LOOPfetch cursor into name-SYS_REFCURSOR只能通过fetch into来打开和遍历 exit when cu

10、rsor%NOTFOUND; -SYS_REFCURSOR中可使用三个状态属性: -%NOTFOUND(未找到记录信息) %FOUND(找到记录信息) -%ROWCOUNT(然后当前游标所指向的行位置)dbms_output.putline(name);end LOOP;rsCursor := cursor;end test;下面写一个简单的例子来对以上所说的存储过程的用法做一个应用:现假设存在两张表,一张是学生成绩表(studnet),字段为:stdId,math,article,language,music,sport,total,average,step 一张是学生课外成绩表(out_s

11、chool),字段为:stdId,parctice,comment通过存储过程自动计算出每位学生的总成绩和平均成绩,同时,如果学生在课外课程中获得的评价为A,就在总成绩上加20分。create or replace procedure autocomputer(step in number) isrsCursor SYS_REFCURSOR;commentArray myPackage.myArray;math number;article number;language number;music number;sport number;total number;average number;

12、stdId varchar(30);record myPackage.stdInfo;i number;begini := 1;get_comment(commentArray); -调用名为get_comment()的存储过程获取学生课外评分信息OPEN rsCursor for select stdId,math,article,language,music,sport from student t where t.step = step;LOOPfetch rsCursor into stdId,math,article,language,music,sport; exit when r

13、sCursor%NOTFOUND;total := math + article + language + music + sport;for i in mentArray.count LOOPrecord := commentArray(i);if stdId = record.stdId thenbeginif ment = 'A' thenbegintotal := total + 20;go to next; -使用go to跳出for循环 end;end if;end;end if;end LOOP; average := total / 5;update stu

14、dent t set t.total=total and t.average = average where t.stdId = stdId;end LOOP;end;end autocomputer;-取得学生评论信息的存储过程create or replace procedure get_comment(commentArray out myPackage.myArray) isrs SYS_REFCURSOR;record myPackage.stdInfo;stdId varchar(30);comment varchar(1);i number;beginopen rs for select stdId,comment from out_schooli := 1;LOOPfetch rs into stdId,comment; exit when rs%NOTFOUND;record.stdId := std

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 试题/考题 > 初中试题/考题

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号