当前所在位置:珠峰网资料 >> 计算机 >> Oracle认证 >> 正文
Oracle创建程序包(三)
发布时间:2010/12/9 10:34:37 来源:www.xue.net 编辑:城市总裁吧
     Exception

  When No_Data_Found Then

  DBMS_output.Put_Line('编号为:'||NewStudent.stuID||'的记录不存在,修改失败');

  When Others Then

  DBMS_output.Put_Line('执行修改操作时发生意外情况,修改未成功');

  End UpdateStudent;--结束修改过程

  /*实现删除过程的定义*/

  Procedure DeleteStudent(DelID in Student.stuid%type) as

  iRec Integer;

  Begin

  Select Count(*) into iRec From Student Where stuID=DelID;

  IF iRec=0 Then

  DBMS_output.Put_Line('编号为:'||DelID||'的记录不存在,删除操作时未成功');

  ELSE

  Delete From student Where   stuid=DelID;

  Commit;

  DBMS_output.Put_Line('删除成功!');

  End IF;

  Exception

  When Others Then

  DBMS_output.Put_Line('执行删除操作时发生意外情况,删除未成功');

  End DeleteStudent;

  /*实现参数带有游标类型定义*/

  Procedure ReturnStudent(inOutstu in out curRefStudent) as

  Begin

  Open inOutstu For Select * from student;

  End ReturnStudent;

  /*实现函数定义*/

  Function RegurnRecordCount Return Number as

  RecCount number(10);

  Begin

  Select Count(*) into RecCount From student;

  Return recCount;

  Exception

  When Others Then

  DBMS_output.Put_Line('查询表中记录数时发生意外情况');

  End RegurnRecordCount; --结束函数的定义

  End studentPackage;--结束包

  示例3:调用包

  1. 调用studentPackage包中的InsertStudent过程

  Declare

  newStu Student%RowType;

  Begin

  newStu.stuID:='1001';

  newStu.stuName:='张三';

  newStu.sex:='男';

  studentPackage.InsertStudent(newStu);

  End;

  /

  2. 调用studentPackage包中的UpdateStudent过程

  Declare

  newStu Student%RowType;

  Begin

  newStu.stuID:='1001';

  newStu.stuName:='李四';

  newStu.sex:='女';

  studentPackage.UpdateStudent(newStu);

  Exception

  When Dup_Val_On_Index Then

  DBMS_output.Put_Line('唯一约束被破坏');

  When Others Then

  DBMS_output.Put_Line('更新过程出现错误');

  End;

  /

  3. 调用studentPackage包中的DeleteStudent过程

  Begin

  studentPackage.DeleteStudent('1001');

  End;

  /

  4. 调用studentPackage包中的ReturnRecordCount函数

  Begin

  DBMS_output.put_Line(studentPackage.ReturnRecordCount);

  End;

  /

广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved