oracle 存储过程小总结

1 创造存储过程

Create or procedure My_procedure( param1,param2) is

Begin

.

.

.

End

2 判断语句

If x>0 then

Begin

.

.

.

End

End if

3 for 循环

For …in… Loop

.

.

.

End Loop

4 循环遍历游标

Create or replace procedure  My_procedure() as Cursor cursor is select name from student;name Varchar(20);

Begin

For name in cursor LOOP

Begin

.

.

.

End

End Loop

End

5 循环遍历数组

Create or replace procedure My_procedure(varArray in My_Package.TestArray) as

I number

Begin

For I in varArray.count Loop

Begin

.

.

.

End

End Loop

End

6 while 循环

While(条件语句) Loop

Begin

.

.

.

End

End Loop

Eg:

Create or replace procedure My_procedure() as

I number

Begin

While(i<10) Loop

Begin

I:=i+1;

End;

End Loop

End

7 oracle 数组的使用

(1)     使用oracle 自带的数组类型

X array //使用的时候进行初始化

Eg:

Create or replace procedure My_procedure(y out array) as

X array;

Begin

X:=new array();

y:=x;

end

(2)     使用自定义的数组类型(自定义数组类型的时候,建议通过创建Package的方式实现,以便于管理)

Eg:

Create or replace package My_package is

Public type declarations type info is record( name varchar(20), y number);

Type TestArray is table of info index by binary_integer;

//如果不指定index by binary_integer 就要 varArray  My_package.TestArray;

varArray:=new My_package.TestArray();

8 oracle 游标的使用

游标的使用是非常有用的额,用于遍历临时表中的查询结果。其相关的方法和属性也是很多

(1)

Create or replace procedure My_procedure () is cursor_1 Cursor is select stu_name form student ;varName varchar(20);

Cursor_2 cursor;

Begin

Select stu_name into cursor_2 from student;

For varName in cursor_2 Loop

Begin

.

.

.

End

End Loop

End

(3)     Sys_refcursor 型游标,该游标是Oracle以预定义的游标,可以作传出参数进行传递

Create or replace procedure My_procedure(rsCursor out Sys_refcursor) is

Cursor_1 Sys_refcursor;

Name varchar(20);

Begin

Open cursor_1 for select stu_name from student //sys_refcursor 只能通过open 方法打开和赋值

Loop

Fetch cursor_1 into name //sys_refcursor 只能通过fetch into 来打开和遍历

Exit when

Cursor_1%notfound  //notfound 未找到记录信息 found找到记录信息 rowcount 当前游标所指向的行的位置

.

.

.

End Loop

rsCursor:=cursor_1

End

友情提示

&apos; 单引号

Eg:

实例

下面写一个简单的例子来对oracle 语句的用法做一个简单的应用

现假设有两张表

一张是学生成绩表(student)

字段为:

stu_id ,math, article, language,music, sport, total,average,step

另一张是学生的课外成绩表(out_school),字段为

Stu_id ,practice,comment

现通过存储过程自动计算出每位学生的总成绩和平均成绩,同时,如果学生在课外课程中获得的评分为“A” ,就在总成绩上面加20分

Create or replace procedure auto_Compute(step in number) as

rsCursor Sys_refcursor;

comentArray myPackage.myArray;

math number;

article number;

language number;

music number;

sport number;

total number;

average number;

stu_id varchar(30);

record myPackage.stdInfo;

I number;

Begin

I;=1;

Get_comment(commentArray);

Open rsCursor for select stu_id ,math, article,language, music,sport from student t where t.step=step;

Loop

Fetch rsCursor into stu_id,math,article,language,music,sport ;//解析rscursor

Exit when rsCursor%notfound;

Total:=math+article+language+music+sport;

For I in commentArray.count Loop

Record:=commentArray(i);

If(stu_id=record.stu.id) then

Begin

If record.comment=’A’ then

Begin

Total:=total+20;

End;

End if

End

Endif

End Loop

Average:total/5;

Update student t set t.total=total and t.average=average where t.stu_id=stu_id;

End Loop;

End;

获得学生的评论信息放入commentArray 数组

Create or replace procedure get_comment(commentArray out myPackage.myArray) as

rsCursor Sys_refCursor;

record myPackage.stdInfo;

std_id varchar(30)

comment varchar(1);

I number;

Begin

Open rscursor for select std_id ,comment from out_school

I:=1;//定义索引

Loop

Fetch rscursor into std_id,comment;

Exit when rscursor%notfound

Record.stu_id=stu_id;

Record.comment=comment;

commentArray(i)=record;

i:=i+1;

end Loop

end;

定义数组类型myArray和评论信息类型stdInfo

Create or replace package myPackage as

Begin

Type stdInfo is record(stu_id varchar(30),comment varchar(1)));

Type myArray is table of stdInfo index by binary_integer;

End ;

时间: 2024-08-29 16:44:58

oracle 存储过程小总结的相关文章

oracle存储过程实例

oracle存储过程实例 分类: 数据(仓)库及处理 2010-05-03 17:15 1055人阅读 评论(2)收藏 举报 认识存储过程和函数 存储过程和函数也是一种PL/SQL块,是存入数据库的PL/SQL块.但存储过程和函数不同于已经介绍过的PL/SQL程序,我们通常把PL/SQL程序称为无名块,而存储过程和函数是以命名的方式存储于数据库中的.和PL/SQL程序相比,存储过程有非常多长处,详细归纳例如以下: * 存储过程和函数以命名的数据库对象形式存储于数据库其中.存储在数据库中的长处是非

Oracle存储过程的编写经验与优化措施

1.开发人员如果用到其他库的Table或View,务必在当前库中建立View来实现跨库操作,最好不要直接使用"databsevv.dbo.table_name",因为sp_depends不能显示出该SP所使用的跨库table或view,不方便校验. 2.开发人员在提交SP前,必须已经使用set showplan on分析过查询计划,做过自身的查询优化检查. 3.高程序运行效率,优化应用程序,在SP编写过程中应该注意以下几点: a) SQL的使用规范: i. 尽量避免大事务操作,慎用ho

oracle的环境配置-oracle的小版本升级

oracle的小版本升级:从10.2.0.1升级到10.2.0.4 需要准备的升级包:p6810189_10204_Linux-x86 1.当前版本 SQL> conn /as sysdbaConnected.SQL> select * from v$version; BANNER----------------------------------------------------------------Oracle Database 10g Enterprise Edition Relea

oracle存储过程的例子

oracle存储过程的例子 分类: 数据(仓)库及处理 2010-05-03 17:15 1055人阅读 评论(2)收藏 举报 认识存储过程和函数 存储过程和函数也是一种PL/SQL块,是存入数据库的PL/SQL块.但存储过程和函数不同于已经介绍过的PL/SQL程序,我们通常把PL/SQL程序称为无名块.而存储过程和函数是以命名的方式存储于数据库中的.和PL/SQL程序相比.存储过程有非常多长处.详细归纳例如以下: * 存储过程和函数以命名的数据库对象形式存储于数据库其中.存储在数据库中的长处是

oracle存储过程 调优 基础篇

1.如果用到其他库的Table或View,务必在当前库中建立View来实现跨库操作,最好不要直接使用"databsevv.dbo.table_name",因为sp_depends不能显示出该SP所使用的跨库table或view,不方便校验. 2.开发人员在提交SP前,必须已经使用set showplan on分析过查询计划,做过自身的查询优化检查. 3.高程序运行效率,优化应用程序,在SP编写过程中应该注意以下几点: a) SQL的使用规范: i. 尽量避免大事务操作,慎用holdlo

ORACLE存储过程调用Web Service

1. 概述 最近在ESB项目中,客户在各个系统之间的服务调用大多都是在oracle存储过程中进行的,本文就oracle存储过程调用web service来进行说明.其他主流数据库,比如mysql和sql service,调用web service的方法这里就不做介绍了,本文主要用来介绍oracle存储过程调用Web Service的方法. 众所周知,在Web Service通过HTTP协议发送请求和接收结果时,发送的请求内容和结果内容都采用XML格式封装,并增加了一些特定的HTTP消息头,以说明

mybatis 操作存储过程 小细节错误

项目开发的时候遇到一个错误: <span style="font-size:24px;">org.springframework.jdbc.BadSqlGrammarException: ### Error querying database. Cause: java.sql.SQLException: ORA-06550: 第 8 行, 第 4 列: PLS-00103: 出现符号 ";"在需要下列之一时: . ( ) , * @ % & =

如何在pl/sql developer 7运行到oracle存储过程设置断点的地方

如何高效调试oracle存储过程,尤其是父子网状调用的存储过程 1,在需要设置断点的oracle存储过程处设置断点         如何设置断点:直接在某行oracle存储过程处单击行首,会在行首显示一个 中间以白叉填充的红色小圆圈. 如何取消断点:单击中间以白叉填充的红色小圆圈即可,断点就消失了     2,然后开启test窗口 3,按上test窗口的放大镜图标(start debugger)或者点击快捷键F9或者依次打开 菜单debug-->start 4,最后再次点击test窗口的run按

C#调用Oracle存储过程的方法

本文实例讲述了C#调用Oracle存储过程的方法.分享给大家供大家参考.具体实现方法如下: Oracle数据库代码如下: 代码如下: create or replace procedure proce_test(paramin in varchar2,paramout out varchar2,paraminout in out varchar2) as varparam varchar2(28); begin varparam:=paramin; paramout:=varparam|| pa