oracle存储过程简单实例 变量赋值 游标遍历
2014-11-22 21:08
369 查看
应用场景:
有两张表,学生表和对应的各科成绩表。
学生表student
字段: id int
name varchar(20)
数值: 1 A
2
B
成绩表score
字段: id int studentid int subjectid int score int
数值:
1 1 1 80
2 1 2 90
3 1 3 100
4 2 1 60
5 2 2 70
用存储过程来通过名字获取对应学生的成绩最大值的科目名称。本例的逻辑比较简单,用一句sql就可以实现,这里只是演示存储过程的基本语法。
创建包package:
create or replace package max_type as
type max_cursor is ref cursor;
end;
创建存储过程procedure:
create or replace procedure getMaxScore
(
xname in varchar,
x_cur out MAX_TYPE.max_cursor
)
as
max_score number;
cur_score number;
cursor cur_1 is select score from score where studentid=(select id from student where name=xname);
begin
max_score := 0;
for everyrow in cur_1 loop
begin
if (everyrow.score > max_score) then
max_score := everyrow.score;
end if;
end;
end loop;
open x_cur for select max_score from student where rownum=1;
end getMaxScore;
调用存储过程:
SQL>var a refcursor;
SQL>call getMaxScore('A', :a);
SQL>print a
输出结果:
100
有两张表,学生表和对应的各科成绩表。
学生表student
字段: id int
name varchar(20)
数值: 1 A
2
B
成绩表score
字段: id int studentid int subjectid int score int
数值:
1 1 1 80
2 1 2 90
3 1 3 100
4 2 1 60
5 2 2 70
用存储过程来通过名字获取对应学生的成绩最大值的科目名称。本例的逻辑比较简单,用一句sql就可以实现,这里只是演示存储过程的基本语法。
创建包package:
create or replace package max_type as
type max_cursor is ref cursor;
end;
创建存储过程procedure:
create or replace procedure getMaxScore
(
xname in varchar,
x_cur out MAX_TYPE.max_cursor
)
as
max_score number;
cur_score number;
cursor cur_1 is select score from score where studentid=(select id from student where name=xname);
begin
max_score := 0;
for everyrow in cur_1 loop
begin
if (everyrow.score > max_score) then
max_score := everyrow.score;
end if;
end;
end loop;
open x_cur for select max_score from student where rownum=1;
end getMaxScore;
调用存储过程:
SQL>var a refcursor;
SQL>call getMaxScore('A', :a);
SQL>print a
输出结果:
100
相关文章推荐
- SQLServer存储过程简单实例 变量赋值 游标遍历
- mysql存储过程简单实例 变量赋值 游标遍历
- SQL游标遍历时的变量赋值
- 数据库技术_Orcale技术(0002)_5分钟会用存储过程_存储过程简单实例(包含循环、条件、增改查、参数传入、变量赋值、java调用等)
- mysql存储过程+游标循环遍历 判断 赋值 等实例一
- 简单oracle存储过程,使用游标
- plsqlus 游标的简单实例
- SQL表变量与临时表区别 + 临时表遍历(非游标)
- 【转】解压缩版tomcat配置及使用(环境变量设置及测试,一个简单的web应用实例)
- C#调用oracle存储过程 最简单的实例
- 实例final变量和静态final变量变量赋值
- Oracle存储过程简单实例
- 简单的游标遍历数据
- JavaScript基础篇之变量作用域、传值、传址的简单介绍与实例
- C#调用oracle存储过程 最简单的实例
- Oracle存储过程简单实例
- oracle和sqlserver在过程sql中通过select对变量进行赋值的区别及实例
- C#调用oracle存储过程 最简单的实例
- (oracle学习笔记)游标实例:通过游标遍历合并数据
- SQL游标简单了解——虚拟DID计费程序实例问题