当前位置: 移动技术网 > IT编程>数据库>Oracle > Oracle 存储过程总结(一、基本应用)

Oracle 存储过程总结(一、基本应用)

2017年12月12日  | 移动技术网IT编程  | 我要评论

迪斯尼动画电影下载,二硫化钼的作用,孙田广美

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 test(workdate in date) is
begin
dbms_output.putline('the input date is:'||to_date(workdate,'yyyy-mm-dd'));
end test;
2、变量赋值
变量名 := 值;
e.g:
create or replace procedure test(workdate in date) is
x number(4,2);
begin
x := 1;
end test;
3、判断语句:
if 比较式 then begin end; end if;
e.g
create or replace procedure test(x in number) is
begin
if x >0 then
begin
x := 0 - x;
end;
end if;
if x = 0 then
begin
x: = 1;
end;
end if;
end test;
4、for 循环
for ... in ... loop
--执行语句
end loop;
(1)循环遍历游标
create or replace procedure test() as
cursor cursor is select name from student; name varchar(20);
begin
for name in cursor loop
begin
dbms_output.putline(name);
end;
end loop;
end test;
(2)循环遍历数组
create or replace procedure test(vararray in mypackage.testarray) as
--(输入参数vararray 是自定义的数组类型,定义方式见标题6)
i number;
begin
i := 1; --存储过程数组是起始位置是从1开始的,与java、c、c++等语言不同。因为在oracle中本是没有数组的概念的,数组其实就是一张
--表(table),每个数组元素就是表中的一个记录,所以遍历数组时就相当于从表中的第一条记录开始遍历
for i in 1..vararray.count loop
dbms_output.putline('the no.'|| i || 'record in vararray is:'||vararray(i));
end loop;
end test;
5、while 循环
while 条件语句 loop
begin
end;
end loop;
e.g
create or replace procedure test(i in number) as
begin
while i < 10 loop
begin
i:= 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) is
x array;
begin
x := 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的类型数据,其实其为一张存储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是非常有用的,用于遍历临时表中的查询结果。其相关方法和属性也很多,现仅就常用的用法做一二介绍:
(1)cursor型游标(不能用于参数传递)
create or replace procedure test() is
cusor_1 cursor is select std_name from student where ...; --cursor的使用方式1 cursor_2 cursor;
begin
select class_name into cursor_2 from class where ...; --cursor的使用方式2
可使用for x in cursor loop .... end loop; 来实现对cursor的遍历
end test;
(2)sys_refcursor型游标,该游标是oracle以预先定义的游标,可作出参数进行传递
create or replace procedure test(rscursor out sys_refcursor) is
cursor sys_refcursor; name varhcar(20);
begin
open cursor for select name from student where ... --sys_refcursor只能通过open方法来打开和赋值
loop
fetch cursor into name --sys_refcursor只能通过fetch into来打开和遍历 exit when cursor%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_school),字段为:stdid,parctice,comment
通过存储过程自动计算出每位学生的总成绩和平均成绩,同时,如果学生在课外课程中获得的评价为a,就在总成绩上加20分。
create or replace procedure autocomputer(step in number) is
rscursor sys_refcursor;
commentarray mypackage.myarray;
math number;
article number;
language number;
music number;
sport number;
total number;
average number;
stdid varchar(30);
record mypackage.stdinfo;
i number;
begin
i := 1;
get_comment(commentarray); --调用名为get_comment()的存储过程获取学生课外评分信息
open rscursor for select stdid,math,article,language,music,sport from student t where t.step = step;
loop
fetch rscursor into stdid,math,article,language,music,sport; exit when rscursor%notfound;
total := math + article + language + music + sport;
for i in 1..commentarray.count loop
record := commentarray(i);
if stdid = record.stdid then
begin
if record.comment = 'a' then
begin
total := total + 20;
go to next; --使用go to跳出for循环
end;
end if;
end;
end if;
end loop;
<<continue>> average := total / 5;
update student 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) is
rs sys_refcursor;
record mypackage.stdinfo;
stdid varchar(30);
comment varchar(1);
i number;
begin
open rs for select stdid,comment from out_school
i := 1;
loop
fetch rs into stdid,comment; exit when rs%notfound;
record.stdid := stdid;
record.comment := comment;
recommentarray(i) := record;
i:=i + 1;
end loop;
end get_comment;
--定义数组类型myarray
create or replace package mypackage is begin
type stdinfo is record(stdid varchar(30),comment varchar(1));
type myarray is table of stdinfo index by binary_integer;
end mypackage;

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网