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 ;

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

  1. 写的一个ORACLE存储过程小练习

    CREATE OR REPLACE PROCEDURE PRO_1112(O_NOTE OUT NUMBER,O_RESULT OUT VARCHAR2)ASV_NO NUMBER(20);V_NOT ...

  2. ORACLE存储过程调用Web Service

    1. 概述 最近在ESB项目中,客户在各个系统之间的服务调用大多都是在oracle存储过程中进行的,本文就oracle存储过程调用web service来进行说明.其他主流数据库,比如mysql和sq ...

  3. oracle存储过程实例

    oracle存储过程实例 分类: 数据(仓)库及处理 2010-05-03 17:15 1055人阅读 评论(2)收藏 举报 认识存储过程和函数 存储过程和函数也是一种PL/SQL块,是存入数据库的P ...

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

    如何高效调试oracle存储过程,尤其是父子网状调用的存储过程 1,在需要设置断点的oracle存储过程处设置断点         如何设置断点:直接在某行oracle存储过程处单击行首,会在行首显示 ...

  5. oracle存储过程的例子

    oracle存储过程的例子 分类: 数据(仓)库及处理 2010-05-03 17:15 1055人阅读 评论(2)收藏 举报 认识存储过程和函数 存储过程和函数也是一种PL/SQL块,是存入数据库的 ...

  6. [oracle] Oracle存储过程里操作BLOB的字节数据的办法,例如写入32位整数

    作者: zyl910 一.缘由 BLOB是指二进制大对象,也就是英文Binary Large Object的缩写. 在很多时候,我们是通过其他编程语言(如Java)访问BLOB的字节数据,进行字节级的 ...

  7. Oracle创建表语句(Create table)语法详解及示例、、 C# 调用Oracle 存储过程返回数据集 实例

    Oracle创建表语句(Create table)语法详解及示例 2010-06-28 13:59:13|  分类: Oracle PL/SQL|字号 订阅 创建表(Create table)语法详解 ...

  8. Oracle存储过程(增、删、改)写法、oracle执行存储过程

    Oracle存储过程(增.删.改)写法 发布时间: 2010-3-24 11:07    作者: ZHF    来源: 51Testing软件测试网采编 字体:  小  中  大  | 上一篇 下一篇 ...

  9. ORACLE存储过程,循环语法和游标

    1.定义所谓存储过程(Stored Procedure),就是一组用于完成特定数据库功能的SQL语句集,该SQL语句集经过编译后存储在数据库系统中.在使用时候,用户通过指定已经定义的存储过程名字并给出 ...

随机推荐

  1. Apache2.4权限配置(原创帖-转载请注明出处)

    ==================说在前面的话================= 1:这次实验使用的php项目是Discuz,Discuz的安装请参照:http://www.cnblogs.com/ ...

  2. 微信小程序实例

    看到小程序,那么火,自己也想动手写一个.但是没有很好的api接口.有一天看到一个开发安卓的朋友,写了一个干货集中营的小程序.就搜了一下.看到api是免费开放的.于是自己也动手写了一个. 具体的微信小程 ...

  3. easyUI的Dialog和Windows框的应用

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  4. java.lang.classNotFound:明明已经导入了jar包,包里也有该类,却找不到的解决方法

    试一下:在web-inf文件夹下新建lib文件夹:将所有需要用到的jar包放在lib中,重启tomcat.

  5. mysql 唯一约束

    ALTER TABLE user ADD UNIQUE (username,userid) 对表user增加username和userid的唯一约束 ALTER TABLE tablename  AD ...

  6. [OC] Podfile 格式内容

    platform :ios, '8.0' target :'targetName' do pod 'Masonry', '~> 1.0.1' pod 'SDCycleScrollView', ' ...

  7. 调度系统任务创建---创建一个JoinTrigger的依赖任务(五)

    有时候我们需要创建一个任务,这个任务有多个下游任务,在所有下游任务执行成功后再触发一个join操作. 这种场景可以使用JoinTrigger的触发器来实现. 该场景对应的拓扑结构如下: 该触发器的详细 ...

  8. css Animation初体验

    目前有越来越多的网站都使用animation,无论他们是用GIF,SVG,WebGL,视频背景或者其他形式展示.适当地使用动画会让网站更生动,互动性更好,为用户增加一个额外的反馈和体验层. 在本教程中 ...

  9. mySQL 50个查询系列

    http://bubufx.com/detail-1749088.html http://www.jb51.net/article/67932.htm Student(S#,Sname,Sage,Ss ...

  10. javascript高级编程3第三章:基本概念 本章内容 语法 数据类型 流控制语句 函数

    3.1 语法 ECMAScript的语法大量借鉴了C及其他类C语言的语法. 3.1.1 区分大小写 3.1.2 标识符 所谓标识符,就是值变量.函数.属性的名字,或者函数的参数.标识符可以是按照下列格 ...